[SOLUTION] How to get ACF fields on templates in sage10

So you want to use ACF fields in your template and created a repeater and some regular fields and when you go to the template and you want to know what to do, sage10 has some new tricks to learn. I learn them the tuff way, but i will leave here my discoveries so you can have a starting point and then the sage rockstars can teach us how to improve this, if necessary.

step 1

protected $acf = true; doesn’t work anymore, instead of that you need to use Acorn

<?php

    namespace App\View\Composers;

    use Roots\Acorn\View\Composer;
    use Roots\Acorn\View\Composers\Concerns\AcfFields;

    class App extends Composer {

        use AcfFields;

    /**
     * List of views served by this composer.
     *
     * @var array
     */
    protected static $views = [
        '*',
    ];

    /**
     * Data to be passed to view before rendering.
     *
     * @return array
     */

    public function with()
    {
        return [
            'fields' => collect($this->fields())->toArray()
        ];
    }

}

Notice the important parts:

  • you need to use Roots\Acorn\Concerns\AcfFields
  • instead of protected $acf = true you now need to type use AcfFields
  • thanks to the new view composer method, you will return in your with function the fields array with all the ACF stuff.

Please notice the following, that is very important:

the $fields variable is an Illuminate\Support\Fluent type of object, not an array or an object so you need to collect the data and prepare it as you liked it. in my case i did it as an array, so you can access the data in the template like:

 @foreach($fields['acf_repeater'] as $object)
     {{$object['property']['value_you_want_to_print']}}
 @endforeach

I hope this serves you as a starting point. I had to learn a lot of laravel to realized where was my error

3 Likes

Cool. I didn’t know this was built in and rolled my own.

I’m a little surprised how it was done in the AcfFields Concern. It seems like it’d be easier if it didn’t extract the array from their mapped collection and instead returned it directly. This could then be simplified even more.

Since Roots offers us Collections, I figured it’d be a good way to illustrate how you can prep the data in a more fluent way in the composer to make it cleaner in the view.

ViewComposer

<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;
use Roots\Acorn\View\Composers\Concerns\AcfFields;

class App extends Composer
{
    use AcfFields;

    protected static $views = [
        '*',
    ];

    public function with()
    {
        return [
            'acf_repeaters' => $this->getAcfRepeaters(),
        ];
    }

    public function getAcfRepeaters()
    {
        return collect($this->fields()['acf_repeater'])
            ->map(function($acf_repeater) {
                return $acf_repeater->property->value_you_want_to_print;
            })->toArray();
    }
}

View

@foreach($acf_repeaters as $acf_repeater)
    {{ $acf_repeater }}
@endforeach

[Edit: Tiny array issue because I obviously didn’t test first]

This topic was automatically closed after 42 days. New replies are no longer allowed.