acf-image-aspect-ratio-crop icon indicating copy to clipboard operation
acf-image-aspect-ratio-crop copied to clipboard

Rest API throws "Fatal Error: Call to undefined method stdClass::format_value_for_rest()"

Open kdechant opened this issue 2 years ago • 5 comments

When using an "Image Aspect Ratio Crop" field that is exposed to the WP REST API, I get a PHP error.

GET http://my.site.domain/wp-json/wp/v2/pages returns this:

Fatal error: Uncaught Error: Call to undefined method stdClass::format_value_for_rest() in /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php:86 --   | Stack trace:   | #0 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php(982): acf_format_value_for_rest('44100', 13663, Array)   | #1 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php(86): acf_field_repeater->format_value_for_rest(Array, 13663, Array)   | #2 /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/class-acf-rest-api.php(234): acf_format_value_for_rest(Array, 13663, Array, 'light')   | #3 /path/to/site/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(439): ACF_Rest_Api->load_fields(Array, 'acf', Object(WP_REST_Request), 'page')   | #4 /path/to/site/wp in /path/to/site/wp-content/plugins/advanced-custom-fields-pro/includes/rest-api/acf-rest-api-functions.php on line 86

This happens when I use the following plugins together:

  • advanced-custom-fields-pro v5.12.5 or v6.1.6
  • acf-repeater v2.0.1
  • acf-image-aspect-ratio-crop v6.0.2

There is a Field Group with a field of type "Repeater". One of the sub-fields of the repeater is the "Image Aspect Ratio Crop" field. The Field Group has the "Show in REST API" setting turned on.

The error also appears when I try to edit content in the WP admin.

The error doesn't occur when using ?acf_format=standard in the query string. (e.g., GET http://my.site.domain/wp-json/wp/v2/pages?acf_format=standard) but that doesn't fix the WP admin.

It appears that this occurs because the plugin doesn't call acf_register_field_type to register the field class here: https://github.com/joppuyo/acf-image-aspect-ratio-crop/blob/e9c4c119168042234884f3ba09ecc21440a9ff9e/fields/class-npx-acf-field-image-aspect-ratio-crop-v5.php#L1138

kdechant avatar Jun 27 '23 00:06 kdechant

Changing the "initialize" part in class-npx-acf-field-image-aspect-ratio-crop-v5.php to this seems to fix it:

// initialize
$instance = new npx_acf_field_image_aspect_ratio_crop($this->settings);
acf_register_field_type($instance);

This is how the other plugins register their fields, including the fields built in to ACF itself. Anyone know if there is any reason that wouldn't work?

kdechant avatar Jun 27 '23 00:06 kdechant

Hey, thanks for the detailed bug report. Unfortunately I have very limited time to maintain this plugin but I will try to fix this issue as soon as possible.

joppuyo avatar Jun 27 '23 08:06 joppuyo

@joppuyo I've created a pull request that resolves the issue based on @kdechant's feedback

jameelmoses avatar Jun 30 '23 20:06 jameelmoses

I have reserved some time next week to look through all issues in this plugin and I will also try to solve this issue.

joppuyo avatar Aug 10 '23 13:08 joppuyo

Hey, right now version 6 of this plugin is minimally maintained and it gets only bug fixes. New features like ACF REST API compatibility will be added in version 7.

joppuyo avatar Aug 17 '23 12:08 joppuyo