ACF Groups in Controllers

Hey folks,

I’m new to Sage 9 and am working my way through the learning curve. I’ve built with Wordpress and Genesis and the beans framework but have heard so many good things about Sage that I wanted to start learning it.

I’m struggling with accessing acf fields in a group using the protected $acf = true;

i have setup a FrontPage.php in App/Controllers which has in it:


namespace App\Controllers;

use Sober\Controller\Controller;

class FrontPage extends Controller
{
      protected $acf = true; // this will add all the ACF fields to the $data variable for this page

}

And then in my front-page.blade.php i have added @debug. I can see the it sitting there: home_benefits_1_group [object] is listed under Data. I’ve tried using

<h2>{{ $home_benefits_1_group->$benefit_heading_1 }}</h2>

to get the field inside the group but it will not display anything, just an empty h2 tag.

I tried doing separate functions to get each field from the group but that didn’t seem very efficient and also didn’t work.

I don’t really know how else to access the contents of the home_benefits1_group object.

The objects are part of the a landing page so can’t be looped through like a list etc.

I am able to access and get the values of other fields that are not in a group without problem, but the group is really throwing me!

Where am i going wrong?

Thanks!

It’s probably the second $ that’s messing you up here. Try

<h2>{{ $home_benefits_1_group->benefit_heading_1 }}</h2>

You could also do this to see the whole object and confirm you’re referencing things correctly:

<?php print_r($home_benefits_group); ?>

Yes that works!!! Thank you so much for that, never thought to remove the second $

It all comes with practice and repetition. Controller returns the ACF options as an object, so it needs to be used that way.

Glad you got it working!

1 Like

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