Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kdechant opened this issue Jun 27, 2023 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@kdechant
Copy link

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:

new npx_acf_field_image_aspect_ratio_crop($this->settings);

@kdechant
Copy link
Author

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?

@joppuyo
Copy link
Owner

joppuyo commented Jun 27, 2023

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.

jameelmoses added a commit to jameelmoses/acf-image-aspect-ratio-crop that referenced this issue Jun 30, 2023
@jameelmoses
Copy link

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

@joppuyo
Copy link
Owner

joppuyo commented Aug 10, 2023

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
Copy link
Owner

joppuyo commented Aug 17, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants