acf_block_json_process_fields()
Recursively ensures every field in a set of block-inlined fields has a key, generating field_{block_slug}_{parent_path}_{name} for any that don't. The parent path scopes sub-field keys under their ancestor names so that two fields with the same name at different depths (e.g. a top-level title and a repeater sub-field title) don't end up with the same field key and overwrite each other in the local fields store. Invalid field definitions (missing a name) are skipped and reported via _doing_it_wrong().
No Hooks.
Returns
Array. The processed fields, with keys filled in and invalid entries removed.
Usage
acf_block_json_process_fields( $fields, $block_slug, $block_name, $parent_path );
- $fields(array) (required)
- The fields to process.
- $block_slug(string) (required)
- The sanitized block slug used to build keys.
- $block_name(string)
- The original block name (used for error messages).
Default:'' - $parent_path(string)
- Internal. Underscore-joined ancestor names for the current nesting level.
Default:''
Changelog
| Since 6.8.1 | Introduced. |
acf_block_json_process_fields() acf block json process fields code ACF 6.8.8
function acf_block_json_process_fields( $fields, $block_slug, $block_name = '', $parent_path = '' ) {
$processed = array();
foreach ( $fields as $field ) {
if ( ! is_array( $field ) ) {
continue;
}
if ( empty( $field['name'] ) ) {
$message = sprintf(
/* translators: %s - The block name. */
__( 'A field defined in block "%s" is missing a "name" and will be skipped.', 'acf' ),
$block_name ? $block_name : $block_slug
);
_doing_it_wrong( 'acf_block_json_process_fields', esc_html( $message ), '6.8.1' );
continue;
}
if ( empty( $field['key'] ) ) {
$key_suffix = '' === $parent_path ? $field['name'] : $parent_path . '_' . $field['name'];
$field['key'] = 'field_' . $block_slug . '_' . $key_suffix;
}
$child_path = '' === $parent_path ? $field['name'] : $parent_path . '_' . $field['name'];
if ( ! empty( $field['sub_fields'] ) && is_array( $field['sub_fields'] ) ) {
$field['sub_fields'] = acf_block_json_process_fields( $field['sub_fields'], $block_slug, $block_name, $child_path );
}
if ( ! empty( $field['layouts'] ) && is_array( $field['layouts'] ) ) {
foreach ( $field['layouts'] as $layout_index => $layout ) {
if ( ! empty( $layout['sub_fields'] ) && is_array( $layout['sub_fields'] ) ) {
// Include the layout's name (or its index as a fallback) so identical sub-field names
// across layouts in the same flexible_content field don't collide.
$layout_name = ! empty( $layout['name'] ) ? $layout['name'] : (string) $layout_index;
$layout_path = $child_path . '_' . $layout_name;
$field['layouts'][ $layout_index ]['sub_fields'] = acf_block_json_process_fields( $layout['sub_fields'], $block_slug, $block_name, $layout_path );
}
}
}
$processed[] = $field;
}
return $processed;
}