I’m working on creating blocks with ACF Composer and I have a couple of questions:
I’m currently using the ACF GUI to define my block’s fields. Is this okay, or should I avoid it? Can I get rid of the fields() function entirely without causing issues?
My block has two fields - Heading and Paragraph. How do I access these fields within the controller? Should I create individual functions for each field retrieval, like this:
...
public function with()
{
return [
'heading' => $this->heading(),
'paragraph' => $this->paragraph(),
];
}
...
public function heading()
{
return get_field('heading');
}
public function paragraph()
{
return get_field('paragraph');
}
...
Is there a more efficient or recommended way to handle this?
I’ve never tried defining fields inside of the GUI for a block but I imagine it should be fine. You’ll have to keep the fields() method and have it return an empty array.
For the get_field() stuff, you can either put it in methods like you have in your example or just pass get_field() directly inside of with() like 'heading' => get_field('heading').
If they don’t return a default value or you need to control wrapper markup, then what you’re doing probably makes sense. Otherwise, you can reach for coalescing operators.