REST API Error
-
REST API breaks for me as the format function is missing:
Call to undefined method stdClass::format_value_for_rest()
Cheers
Alexander
-
Hmm, I’ve never tried using this in REST. This was originally built before WP & REST was really a thing so probably a blind spot that just hasn’t been brought up by other users yet. Looking at the latest boilerplate for new ACF fields I don’t see any mention of the format_value_for_rest() function you are seeing errors about. I don’t extend that function in my plugin but you’d think it would be able to access it from the parent ‘acf_field’ class my field extends.
/** * Apply basic formatting to prepare the value for default REST output. * * @param mixed $value * @param string|int $post_id * @param array $field * @return mixed */ public function format_value_for_rest( $value, $post_id, array $field ) { return $value; }
-
This reply was modified 2 years, 2 months ago by
Matt Keys.
Does adding
public $show_in_rest = true;
to the class in eitheracf-font-awesome-v5.php
oracf-font-awesome-v6.php
(depending which FontAwesome you are using) make any difference to the error you are seeing?See the boilerplate I linked above for where they added that line.
public $show_in_rest = true;
does not fix the error.
Seems to be related to the fieldtype not being reported at all.acf_get_field_type( $field['type'] )
Will dive a bit deeper and report back.
Ok found the problem! As mentioned above the fieldtype needs to be registered within
acf-font-awesome-v5.php
oracf-font-awesome-v6.php
$fa = new acf_field_font_awesome( $this->settings );
acf_register_field_type( $fa );Cheers
Nice, that definitely points things in the right direction. A search for that function turns up this page:
https://www.advancedcustomfields.com/resources/integrating-custom-field-types/
This bit here seems to be referring to plugins like mine:
For field types that are not registered with
acf_register_field_type()
, such as field types that just construct themselves inside theacf/include_field_types
action hook, it is possible to include them in the REST API by using theacf/rest/get_fields
filter as shown below:This plugin using the include_field_types action they reference.
Using the filter they suggest on that page will allow you to include FontAwesome into your REST fields without making any modifications to this plugin that would get overwritten during the next plugin update.
I’ll look into what the appropriate way would be for me to include this out of the box in a future update but that should get you what you need for now.
-
This reply was modified 2 years, 2 months ago by
Matt Keys.
With the above changes I mentioned, all is working as it should now and no more exceptions 🙂
ThanksGood to hear, I only mention the filter as it sounds like you made changes within
acf-font-awesome-v5.php
oracf-font-awesome-v6.php
, and those changes will get lost on the next plugin update. If I read that wrong and you did your changes outside of this plugins codebase, then all is well.I tried the hooks above before :), but none of them prevents the exception early enough. So for now I did tweak the plugin files to make it work 😉
scandalous! 😲
I KNOW!! I am really sorry, but I added a big yellow sticky note to my monitor to remember 🙂
-
This reply was modified 2 years, 2 months ago by
- The topic ‘REST API Error’ is closed to new replies.