Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
398 stars 53 forks source link

Nested inner blocks #230

Closed marcbelletre closed 3 months ago

marcbelletre commented 4 months ago

Hi @Log1x,

I would like to build a block that is based on the core/media-text block. I didn't find any documentation about this but after digging into the source code I found out that it was possible to do so using an innerBlock property. My first attempt was to use it like below:

/**
 * The block attributes.
 */
public function attributes(): array
{
    return [
        'template' => [
            // ...
            'core/media-text' => [
                'mediaType' => 'image',
                'mediaWidth' => 25,
                'innerBlocks' => [
                    'core/heading' => ['placeholder' => 'Hello World'],
                    'core/paragraph' => ['placeholder' => 'Welcome to the Team Member block.'],
                ],
            ],
        ],
    ];
}

Because of the line that collapses the collection here it is necessary to wrap each block into a single array to make it work.

'innerBlocks' => [
    [
        'core/heading' => ['placeholder' => 'Hello World'],
    ],
    [
        'core/paragraph' => ['placeholder' => 'Welcome to the Team Member block.'],
    ],
],

If I remove the collapse() method the first approach works fine. So I'm just wondering if it is here for a reason? If not, should we remove it?

Cheers, and thank you for this awesome package! 🚀

Log1x commented 4 months ago

If I remove the collapse() method the first approach works fine.

I think I had to do this due to group/column blocks. Check out https://github.com/Log1x/acf-composer/pull/159 for more details.

This feature wasn't released til v3 so I'm open to changing it/making it better – I just want to make sure it keeps support for everything.

I didn't find any documentation about this

ACF Composer has outgrown the README. I plan to put up real docs soon.

marcbelletre commented 3 months ago

I was about to make some tests to see if uncollapsing the collection was also working with groups and columns. Then I realized there is a very simple reason why we can't use the slugs as keys.

This would not work because of the duplicated key:

'innerBlocks' => [
    'core/heading' => ['placeholder' => 'Hello World'],
    'core/paragraph' => ['placeholder' => 'This is the first paragraph'],
    'core/paragraph' => ['placeholder' => 'This is the second paragraph'],
],

This does:

'innerBlocks' => [
    ['core/heading' => ['placeholder' => 'Hello World']],
    ['core/paragraph' => ['placeholder' => 'This is the first paragraph']],
    ['core/paragraph' => ['placeholder' => 'This is the second paragraph']],
],