Support

Account

Home Forums ACF PRO ACF Block vs. field_type: group – template not working

Solved

ACF Block vs. field_type: group – template not working

  • Hi, I’m having a bit of a problem with <InnerBlock> templating. I have it working fine for the basic cases. However, I have a custom block that uses field type: group to make things neat and tidy for the editors, but I’m having the hardest time setting defaults for that group when I include it as an <InnerBlock>. Here is what I have, which is most likely wrong, but it was my best guesstimate considering how core blocks are included.

    
    $template = array(
        array(
            'acf/custom-button-block',
            array(
                'name' => 'acf/custom-button-block',
                'mode' => 'auto',
                'data'  => array(
                    'button_type' => 'contact_sales',
                    'button_tracking_id'  => 'offer_card_cta_btn',
                    'button_design' => 'light-filled',
                    array('button_size_group', array(
                        'data' => array(
                            'button_size_select' => 'small',
                        ),
                    )),
                    'button_link_url' => '',
                    'button_label' => '',
                    'button_padding' => '',
                    'button_min_width' => '',
                    'button_icon_group' => array(
                        'button_icon_placement' => 'left',
                        'button_icon_selector' => '',
                        'button_icon' => '0',
                    ),
                ),
            )
        )
    );
    

    You can see that I am trying 2 different approaches, one with the button_size_group and then again with the button_icon_group but neither seems to be working

    The error I’m getting when I try to render the block is Warning: Trying to access array offset on null in ...path/to/button-block on line {n} for every field corresponding to a sub-field of the button_size_group as I try to load the field values:

    
    $sizes = get_field('button_size_group');
    $values['size'] = $sizes['button_size_select'];
    $values['padding'] = $sizes['button_padding'];
    $values['min_width'] = $sizes['button_min_width'];
    

    and of course I could make the references with $values['size'] = $sizes['button_size_select'] ?? ''; but that won’t help me when I actually need to load these fields with values

  • Solved: Leaving this up if anybody else has this same issue in the future. It was just me being stupid though, so not really likely to be of use for others to be fair.

    I had not checked how the group field works and how to reference sub-fields in general, so I assumed I had to do some sub-array shenanigans. The solution turned out to just be the standard notation for referencing those values.
    get_field('group_name_subfield_name') to get the field directly without having to get the field group first with an array of subfields
    then to set the default value in a parent block using InnerBlocks it’s just that same key 'group_name_subfied_name' => {value}

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.