AdvancedCustomFields / AdvancedCustomFields/acf
Uncaught Error: Call to undefined method stdClass::format_value_for_rest()
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
External plugin sometimes break the REST api because they don't implement format_value_for_rest. |
Now this is really their fault. But a simple solution is to a function exists check:
method_exists( $type, 'format_value_for_rest' )
/**
* Format a given field's value for output in the REST API.
*
* @param $value
* @param $post_id
* @param $field
* @param string $format 'light' for normal REST API formatting or 'standard' to apply ACF's normal field formatting.
* @return mixed
*/
function acf_format_value_for_rest( $value, $post_id, $field, $format = 'light' ) {
if ( $format === 'standard' && function_exists( 'acf_format_value' ) ) {
$value_formatted = acf_format_value( $value, $post_id, $field );
} else {
$type = acf_get_field_type( $field['type'] );
if ( method_exists( $type, 'format_value_for_rest' ) ) {
$value_formatted = $type->format_value_for_rest( $value, $post_id, $field );
} else {
// Handle the case if neither function nor method exists
$value_formatted = $value; // or set default/fallback
}
}
/**
* Filter the formatted value for a given field.
*
* @param mixed $value_formatted The formatted value.
* @param string|int $post_id The post ID of the current object.
* @param array $field The field array.
* @param mixed $value The raw/unformatted value.
* @param string $format The format applied to the field value.
*/
return apply_filters( 'acf/rest/format_value_for_rest', $value_formatted, $post_id, $field, $value, $format );
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the acf_format_value_for_rest() entry point and inspect how acf_get_field_type() results are used before format_value_for_rest() is called. Reproduce the undefined-method failure with a field type that lacks the method, then add coverage showing the REST value is handled without an uncaught error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100