Advanced custom fields variables using get_sub_field not working

Hello!

I’m declaring my ACF variables in the app controller as per the book, and it seems that when I try to declare a sub field, the value is not returning. Any advice?

In the code below, the intro_heading and intro_body return fine, but anything with the get_sub_field does not return.

I know this does work as I call the sub field manually, it works by using {{ get_sub_field(‘happening_title’) }}

Here is my code that i’m using in my controllers/app.php

public function intro_heading() {
return get_field(‘intro_heading’);
}

public function intro_body() {
return get_field(‘intro_body’);
}

// happening variables

public function happening_content() {
return get_field(‘happening_content’);
}

public function happening_test() {
return get_field(‘happening_test’);
}

public function happening_image() {
return get_sub_field(‘happening_image’);
}

public function happening_title() {
return get_sub_field(‘happening_title’);
}

public function happening_details() {
return get_field(‘happening_details’);
}

public function happening_book_now() {
return get_sub_field(‘happening_book_now’);
}

public function happening_book_now_title() {
return get_sub_field(‘happening_book_now_title’);
}

Thanks for your help!

Per controller’s documentation fields of any kind inside a loop cannot be set the way you’re setting them. The reason being the controller runs when the template is first loaded, and since you’re not looping through the repeater or flexible content field at that time, get_sub_field() won’t return anything.

What you could do is set the repeater field in controller and use its subfields like this:

App.php

public function repeater() {
  return get_field('repeater');
}

page.blade.php (or whatever)

@if($repeater)
  <ul>
  @foreach($repeater as $r)
    <li>{!! $r['sub_field_name'] !!}</li>
  @endforeach
  </ul>
@endif
3 Likes

@bonlando I would recommend upgrading to Controller 2.1.0. The code above is super repetitive. I wouldn’t recommend the above use case, as you’re creating more work and project maintenance for yourself. That was never the goal for Controller.

Controller 2.1.0 has a Advanced Custom Fields module to help with what you’re doing. You can use protected $acf = true; in your App Controller class, and it was automate all of the above. You can then use @code or @codeif in your blade view file to see the Blade output. This may help you get started.

Documentation below

2 Likes

Ah you have been great, thanks!

Follow up question - i’m trying to figure out the proper syntax for an image array or ID within my repeater and i’m not sure where to start.

<img src="{!! $r['happening_image['url']'] !!}" alt="">

certainly isn’t proper syntax, I apologize if i’m being seriously green here…appreciate the help

Thanks @withjacoby - i’ll be looking into that for the next project. This guys due really soon.

I’m not entirely sure what you mean here. If you’re working with an ACF image array, then you’d use something like $r['img_field_name']['url'] but the specifics of your field setup and template will affect this.

A good place to start might be just echo or print_rIng your value and working from there once you know what the data looks like. I’ve been working with Sage and ACF for years and I still regularly echo values while I’m working to see the result and remind myself what I’m working with.