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

$template with multiple of same top level block #239

Open themorgansmake opened 3 months ago

themorgansmake commented 3 months ago

Hi there,

Hope all is well.

Is it possible to use the $template value with multiple of the same top level blocks?

I think the issue is that you specify a key of the name of the block and that overrides the previous one (vanilla php stuff etc).

For example, you will only end up with paragraph 3:

` public $template = [ 'core/paragraph' => ['placeholder' => "1"], 'core/paragraph' => ['placeholder' => "2"], 'core/paragraph' => ['placeholder' => "3"], ];

I also tried wrapping them but I don't think the handleTemplate supports this currently:

public $template = [ ['core/paragraph' => ['placeholder' => "1"]], ['core/paragraph' => ['placeholder' => "2"]], ['core/paragraph' => ['placeholder' => "3"]], ];`

Not sure if I'm just missing something or if it could be supported at some time?

Thanks in advance!

Log1x commented 3 months ago

I believe the latter (wrapping) should work.

https://github.com/Log1x/acf-composer/issues/230 has some more info.

themorgansmake commented 3 months ago

Thanks for the quick reply @Log1x

I could be wrong but I think that might just be for "InnerBlock" functionality.

When wrapping top level blocks, the $block->template in the blade view spits out something like:

[[0,{"core\/paragraph":{"placeholder":"1"}}],[1,{"core\/paragraph":{"placeholder":"2"}}],[2,{"core\/paragraph":{"placeholder":"3"}}]]

Opposed to when you have different block names:

[["core\/paragraph",{"placeholder":"1"}],["core\/heading",{"placeholder":"2"}]]

Log1x commented 3 months ago

ahhh I see. I will have to look into it when I get a moment but it might be an oversight

slackday commented 3 months ago

I noticed this too.

This syntax should be supported

public $template = [
  ['core/paragraph' => ['placeholder' => "1"]],
  ['core/paragraph' => ['placeholder' => "2"]],
  ['core/paragraph' => ['placeholder' => "3"]],
];
slackday commented 3 months ago

Not sure my syntax above is correct after all. I tried both arrays and associative array syntax but nothing worked using the $template property of the block.

I ended up doing the following:

    /**
     * Data to be passed to the block before rendering.
     */
    public function with(): array
    {
        return [
            'allowed_blocks' => esc_attr(wp_json_encode([
                'core/heading',
                'core/paragraph',
            ])),
            'block_template' => esc_attr(wp_json_encode([
                ['core/paragraph', [
                    'placeholder' => '1',
                ]],
                ['core/heading', [
                    'level' => 1,
                    'placeholder' => '2',
                ]],
                ['core/paragraph', [
                    'placeholder' => '3,
                ]],
            ])),
        ];
    }
      <InnerBlocks
        allowedBlocks="{!! $allowed_blocks !!}"
        template="{!! $block_template !!}"
      />
tombroucke commented 2 months ago

Ran into the same issue. You can just use the $template structure as defined in https://www.advancedcustomfields.com/resources/acf-blocks-using-innerblocks-and-parent-child-relationships/#example-of-passing-a-template-to-innerblocks. And then override the handleTemplate() method:

/**
* The block template.
*
* @var array
*/
public $template = [
    [
        'core/group',
        [
            'layout' => [
                'type' => 'constrained',
            ],
        ],
        [
            [
                'core/paragraph',
                [
                    'align'   => 'center',
                    'content' => 'I\'m a paragraph.',
                ],
                [],
            ],
            [
                'core/separator',
                [],
                [],
            ],
        ],
    ],
];

/**
 * Handle the block template.
 */
public function handleTemplate(array $template = []): Collection
{
    return collect($template);
}