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
413 stars 56 forks source link

Confusion with populating the view with field data #128

Closed nathangross closed 1 year ago

nathangross commented 1 year ago

Hi! I'm just starting to dig into ACF Composer (and ACF Builder). When I create a block, I'm a bit unclear on the 'best practice' to access the field data in the view.

Let's say I'm creating a button that has a label. I'd like to show that label in the wp editor preview.

When I create a new block (wp acorn acf:block Button) the Button class has a function called items()

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

Is this just part of the boilerplate—since there is an items field? Would I create a function for each piece of data I'd like to display in the view?

Or is it better to just access the field data in the view with get_field() or even @field() with Sage Directives?

Log1x commented 1 year ago

Yeah, I personally create a method for each piece of data that I want to display in the view. Keeps it nice and clean. I find adding that logic inside of my views is not ideal as I usually require more logic alongside my get_field() such as a default value, type-checking, handling the example/preview data, etc.