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
400 stars 53 forks source link

Adding example data #179

Closed smithd88 closed 9 months ago

smithd88 commented 9 months ago

I'm having a difficult time getting my example data to reference a static image in my asset directory. It works if I define it in a constructor within my block. However, doing so results in the ACF Fields not registering to the block. Here's some code which shows what I'm attempting to do:

class MyBlock extends Block 
{
    // Adding the constructor causes ACF Fields to not be registered to the block
    public function __construct()
    {
        // Defining the example image path in the constructor allows me to call the Roots\asset() method
        $this->example['image'] = [
            'alt' => 'Sample image',
            'url' => asset('images/placeholder.webp'),
        ];
    }

    public $example = [
        'heading' => 'Add a heading...',
        'button' => [
            'text' => 'Button Text',
            'url' => '#',
        ],
     ];

    public function with()
    {
        'heading' => $this->heading(),
        'button' => $this->button(),
        'image' => $this->image(),
    }

    public function fields()
    {
        $builder = new FieldsBuilder('myBlock');
        $builder
             ->addTextarea('heading', [
                'label' => 'Heading',
            ])

            ->addGroup('button', ['label' => 'Button'])
                ->addText('text', [
                    'label' => 'Button Text',
                ])
                ->addText('url', [
                    'label' => 'Button URL',
                ])
            ->endGroup()

            ->addImage('image', [
                'label' => 'Image',
                'return_format' => 'array',
                'preview_size' => 'thumbnail',
            ]);

        return $builder->build();
    }

    public function heading()
    {
        return get_field('heading') ?: $this->example['heading'];
    }

    public function button()
    {
        return get_field('button') ?: $this->example['button'];
    }

    public function image()
    {
        return get_field('image') ?: $this->example['image'];
    }
}
smithd88 commented 9 months ago

This appears to be an order of execution issue. Defining the example values in the fields() method resolves the issue. However, I would love to hear if there's a better approach?

Log1x commented 9 months ago

you have to call the parent constructor.

// Adding the constructor causes ACF Fields to not be registered to the block
public function __construct(Application $app)
{
    // Defining the example image path in the constructor allows me to call the Roots\asset() method
    $this->example['image'] = [
        'alt' => 'Sample image',
        'url' => asset('images/placeholder.webp'),
    ];

    parent::__construct($app);
}

you can see a better example on the construct stub.

smithd88 commented 9 months ago

I attempted this, but I don't think I passed $app back to the parent::__construct(). This worked great. Thank you for taking the time to answer my question!