ACF\Pro\Datastore
Localization::serialize_flexible_content_value
Serializes a flexible content field value.
acf_get_value() returns the loaded value from the flex content's load_value() method -- an array of layout row objects, each containing an 'acf_fc_layout' key and sub-field values keyed by field key.
Method of the class: Localization{}
No Hooks.
Returns
Array. Array of layout objects.
Usage
$Localization = new Localization(); $Localization->serialize_flexible_content_value( $field, $value, $post_id );
- $field(array) (required)
- The flexible content field array.
- $value(mixed) (required)
- The loaded value (array of layout rows from load_value).
- $post_id(int) (required)
- The post ID.
Changelog
| Since 6.8.1 | Introduced. |
Localization::serialize_flexible_content_value() Localization::serialize flexible content value code ACF 6.8.8
public function serialize_flexible_content_value( $field, $value, $post_id ) {
if ( ! is_array( $value ) || empty( $field['layouts'] ) ) {
return array();
}
// Build a lookup of layouts by name for sub-field definitions.
$layouts_by_name = array();
foreach ( $field['layouts'] as $layout ) {
$layouts_by_name[ $layout['name'] ] = $layout;
}
$result = array();
foreach ( $value as $row ) {
if ( ! is_array( $row ) || empty( $row['acf_fc_layout'] ) ) {
continue;
}
$layout_name = $row['acf_fc_layout'];
if ( ! isset( $layouts_by_name[ $layout_name ] ) ) {
continue;
}
$layout = $layouts_by_name[ $layout_name ];
$layout_row = array( 'acf_fc_layout' => $layout_name );
if ( ! empty( $layout['sub_fields'] ) ) {
foreach ( $layout['sub_fields'] as $sub_field ) {
$sub_value = isset( $row[ $sub_field['key'] ] ) ? $row[ $sub_field['key'] ] : null;
$layout_row[ $sub_field['key'] ] = $this->serialize_field_value( $sub_field, $sub_value, $post_id );
}
}
$result[] = $layout_row;
}
return $result;
}