ACF-Composer Block Creation - Need Help with Fields

Hi everyone,

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?

Thanks in advance for any help!

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').

Thank you
One last question, please :slight_smile: :
Do we need to add a check (if-else) for every ACF field we include in a Laravel Blade view?

Example:

@if ($items)
@foreach ($items as $item)
<div>
  <div>
    <div>
      @if ($item['fieldOne'])
      <h2>{{ $item['fieldOne'] }}</h2>
      @endif
    </div>
    @if ($item['fieldTwo'])
    <p>{{ $item['fieldTwo'] }}</p>
    @endif
    @if ($item['fieldThree'])
    <p>{{ $item['fieldThree'] }}</p>
    @endif
  </div>
</div>
@endforeach
@endif

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.

Thank you very much Brandon