
I’ve been adding to the plugin ‘ACF Flexible Content Preview’.
I added code to be able to retrieve keys from nested fields. It works well but leaves and error in the WP Admin on page using/editing Custom Fields.
Error: Notice: Undefined index: ID in /wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php on line 361
The function is:
<?php
/**
* Get all ACF flexible content field layout keys
*
* TODO: Add caching?
*
* @return array
*/
public function retrieve_flexible_keys() {
$keys = [];
$groups = acf_get_field_groups();
if ( empty( $groups ) ) {
return $keys;
}
foreach ( $groups as $group ) {
$fields = (array) acf_get_fields( $group );
if ( empty( $fields ) ) {
continue;
}
foreach ( $fields as $field ) {
if ( 'flexible_content' === $field['type'] ) {
// Flexible is recursive structure with sub_fields into layouts
foreach ( $field['layouts'] as $layout_field ) {
if ( ! empty( $keys [ $layout_field['key'] ] ) ) {
continue;
}
$keys[ $layout_field['key'] ] = $layout_field['name'];
$sub_fields = (array) acf_get_fields($layout_field);
foreach( $sub_fields as $sub_field ) {
if( 'flexible_content' === $sub_field['type'] ) {
foreach( $sub_field['layouts'] as $layout_sub_field ) {
if ( ! empty( $keys [ $layout_sub_field['key'] ] ) ) {
continue;
}
$keys[ $layout_sub_field['key'] ] = $layout_sub_field['name'];
}
}
}
}
}
}
}
return $keys;
}
?>
I think the problematic line is $sub_fields = (array) acf_get_fields($layout_field);
Help appreciated.