Support

Account

Home Forums ACF PRO registering field group adding flex layouts via php

Unread

registering field group adding flex layouts via php

  • Hello ACF friends,

    I am trying to register a field group via PHP and creating the layouts from other field groups, my procedure is as follows:

    
    add_action('acf/init', function() {
    
        $elements = [
            'image_text'
        ];
    
        $layouts = [];
    
        foreach ($elements as $element) {
    
            $acf_json_data = locate_template("inc/acf/exports/group_" . $element . ".json");
            $data = $acf_json_data ? json_decode(file_get_contents($acf_json_data), true) : [];
    
            if( !empty($data) ) {
                $key = 'layout_' . $element;
    
                $layout = [
                    'key' => $key,
                    'name' => $element,
                    'label' => $data[0]['title'],
                    'display' => 'block',
                    'sub_fields' => $data[0]['fields'],
                    'min' => '',
                    'max' => ''
                ];
    
                $layouts[$key] = $layout;
            }
        }
    
        acf_add_local_field_group(array(
            'key' => 'group_main_content',
            'title' => 'Content',
            'fields' => array(
                array(
                    'key' => 'field_main_content_flex_content',
                    'label' => 'Content',
                    'name' => 'flex_content',
                    'type' => 'flexible_content',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'layouts' => $layouts,
                    'button_label' => 'Element hinzufügen',
                    'min' => '',
                    'max' => '',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
            ),
            'menu_order' => 1,
            'position' => 'acf_after_title',
            'style' => 'default',
            'label_placement' => 'left',
            'instruction_placement' => 'label',
            'hide_on_screen' => array(
                0 => 'the_content',
                1 => 'excerpt',
                2 => 'discussion',
                3 => 'comments',
                4 => 'revisions',
                5 => 'slug',
                6 => 'author',
                7 => 'format',
                8 => 'page_attributes',
                9 => 'featured_image',
                10 => 'categories',
                11 => 'tags',
                12 => 'send-trackbacks',
            ),
            'active' => true,
            'description' => 'beschreibung',
        ));
    
    });
    

    The field group I load as layout looks like this:

    
    [
        {
            "key": "group_image_text",
            "title": "Bild und Text",
            "fields": [
                {
                    "key": "field_image_text_image",
                    "label": "Bild",
                    "name": "image",
                    "type": "image",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "return_format": "array",
                    "preview_size": "medium",
                    "library": "all",
                    "min_width": "",
                    "min_height": "",
                    "min_size": "",
                    "max_width": "",
                    "max_height": "",
                    "max_size": "",
                    "mime_types": ""
                },
                {
                    "key": "field_image_text_text",
                    "label": "Text",
                    "name": "text",
                    "type": "text",
                    "instructions": "",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "",
                        "id": ""
                    },
                    "default_value": "",
                    "placeholder": "",
                    "prepend": "",
                    "append": "",
                    "maxlength": ""
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "post"
                    }
                ]
            ],
            "menu_order": 2,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": false,
            "description": ""
        }
    ]
    

    This works quite well so far, and I have 3 questions about it.

    1. does adding layouts make sense like this? Should I do it in a different way, or should I leave it better?

    2. instead of creating the layouts myself, should I rather use the respective field group as a clone field in the layout?

    3. i discovered the following little thing. Through an error I became aware of the validation.php. There I had the problem that in line 403 the filter threw an error, because the key $field[‘_name’] was missing.

    At this point I’ve done a debug output of $field before this line. Than i found the following:

    If the field group is registered using the backend, I have once in the field an ID (51) and a parent (50) and with each Sub Field likewise an ID () and a parent.

    See here:

    
    Array
    (
        [ID] => 51
        [key] => field_flex_content
        [label] => Content
        [name] => flex_content
        [prefix] => acf
        [type] => flexible_content
        [value] => 
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 50
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [layouts] => Array
            (
                [layout_image_text] => Array
                    (
                        [key] => layout_image_text
                        [name] => imageText
                        [label] => Bild & Text
                        [display] => block
                        [sub_fields] => Array
                            (
                                [0] => Array
                                    (
                                        [ID] => 48
                                        [key] => field_image_text_image
                                        [label] => Bild
                                        [name] => imageTextImage
                                        [prefix] => acf
                                        [type] => image
                                        [value] => 
                                        [menu_order] => 0
                                        [instructions] => 
                                        [required] => 0
                                        [id] => 
                                        [class] => 
                                        [conditional_logic] => 0
                                        [parent] => 51
                                        [wrapper] => Array
                                            (
                                                [width] => 
                                                [class] => 
                                                [id] => 
                                            )
    
                                        [parent_layout] => layout_image_text
                                        [return_format] => array
                                        [preview_size] => medium
                                        [library] => all
                                        [min_width] => 
                                        [min_height] => 
                                        [min_size] => 
                                        [max_width] => 
                                        [max_height] => 
                                        [max_size] => 
                                        [mime_types] => 
                                        [_name] => imageTextImage
                                        [_valid] => 1
                                    )
    
                                [1] => Array
                                    (
                                        [ID] => 49
                                        [key] => field_image_text_text
                                        [label] => Text
                                        [name] => imageTextText
                                        [prefix] => acf
                                        [type] => text
                                        [value] => 
                                        [menu_order] => 1
                                        [instructions] => 
                                        [required] => 0
                                        [id] => 
                                        [class] => 
                                        [conditional_logic] => 0
                                        [parent] => 51
                                        [wrapper] => Array
                                            (
                                                [width] => 
                                                [class] => 
                                                [id] => 
                                            )
    
                                        [parent_layout] => layout_image_text
                                        [default_value] => 
                                        [placeholder] => 
                                        [prepend] => 
                                        [append] => 
                                        [maxlength] => 
                                        [_name] => imageTextText
                                        [_valid] => 1
                                    )
    
                            )
    
                        [min] => 
                        [max] => 
                    )
    
            )
    
        [button_label] => Element hinzufügen
        [min] => 
        [max] => 
        [_name] => flex_content
        [_valid] => 1
    )
    

    However, if I register the field via PHP these ID’s are 0 or a string and I wonder if this is a problem:

    
    Array
    (
        [ID] => 0
        [key] => field_main_content_flex_content
        [label] => Content
        [name] => flex_content
        [prefix] => acf
        [type] => flexible_content
        [value] => 
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => group_main_content
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [layouts] => Array
            (
                [layout_image_text] => Array
                    (
                        [key] => layout_image_text
                        [name] => image_text
                        [label] => Bild und Text
                        [display] => block
                        [sub_fields] => Array
                            (
                                [0] => Array
                                    (
                                        [ID] => 0
                                        [key] => field_image_text_image
                                        [label] => Bild
                                        [name] => image
                                        [prefix] => acf
                                        [type] => image
                                        [value] => 
                                        [menu_order] => 0
                                        [instructions] => 
                                        [required] => 0
                                        [id] => 
                                        [class] => 
                                        [conditional_logic] => 0
                                        [parent] => field_main_content_flex_content
                                        [wrapper] => Array
                                            (
                                                [width] => 
                                                [class] => 
                                                [id] => 
                                            )
    
                                        [return_format] => array
                                        [preview_size] => medium
                                        [library] => all
                                        [min_width] => 
                                        [min_height] => 
                                        [min_size] => 
                                        [max_width] => 
                                        [max_height] => 
                                        [max_size] => 
                                        [mime_types] => 
                                        [parent_layout] => layout_image_text
                                        [_name] => image
                                        [_valid] => 1
                                    )
    
                                [1] => Array
                                    (
                                        [ID] => 0
                                        [key] => field_image_text_text
                                        [label] => Text
                                        [name] => text
                                        [prefix] => acf
                                        [type] => text
                                        [value] => 
                                        [menu_order] => 1
                                        [instructions] => 
                                        [required] => 0
                                        [id] => 
                                        [class] => 
                                        [conditional_logic] => 0
                                        [parent] => field_main_content_flex_content
                                        [wrapper] => Array
                                            (
                                                [width] => 
                                                [class] => 
                                                [id] => 
                                            )
    
                                        [default_value] => 
                                        [placeholder] => 
                                        [prepend] => 
                                        [append] => 
                                        [maxlength] => 
                                        [parent_layout] => layout_image_text
                                        [_name] => text
                                        [_valid] => 1
                                    )
    
                            )
    
                        [min] => 
                        [max] => 
                    )
    
            )
    
        [button_label] => Element hinzufügen
        [min] => 
        [max] => 
        [_name] => flex_content
        [_valid] => 1
    )
    

    Thanks for a quick feedback on these questions, and thanks also for your great plugin. 🙂

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.