acf_field_clone::format_value_for_rest
Apply basic formatting to prepare the value for default REST output.
Method of the class: acf_field_clone{}
No Hooks.
Returns
Mixed.
Usage
$acf_field_clone = new acf_field_clone(); $acf_field_clone->format_value_for_rest( $value, $post_id, $field );
- $value(mixed) (required)
- .
- $post_id(string|int) (required)
- .
- $field(array) (required)
- .
acf_field_clone::format_value_for_rest() acf field clone::format value for rest code ACF 6.8.8
public function format_value_for_rest( $value, $post_id, array $field ) {
if ( empty( $value ) || ! is_array( $value ) ) {
return $value;
}
if ( ! is_array( $field ) || ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
return $value;
}
// Loop through each row and within that, each sub field to process sub fields individually.
foreach ( $field['sub_fields'] as $sub_field ) {
// Extract the sub field 'field_key'=>'value' pair from the $value and format it.
$sub_value = acf_extract_var( $value, $sub_field['key'] );
$sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
// Add the sub field value back to the $value but mapped to the field name instead
// of the key reference.
$value[ $sub_field['name'] ] = $sub_value;
}
return $value;
}