ACF PRO - Repeater (get_sub_field) + get_field

It is my first week with sage9, so many new things to learn, but still happy with the choice to use this new workflow (sage9+trellis+bedrock).
I bought the book that you wrote, and I really love it.

I am working on the website of my agency.
I created a custom post type: “work” with these fields:

  • work_logo
  • work_client_name
  • work_title
  • work_description
  • work_block (repeater) > with subfields

this is a screenshot of my custom field “work”

I followed what the book says and I created app/controllers/single-work.php with this code

<?php

namespace App;

use Sober\Controller\Controller;

class SingleWork extends Controller
{
		public function work_logo() {
			return get_field('work_logo');
		}
		public function work_client_name() {
			return get_field('work_client_name');
		}
		public function work_title() {
			return get_field('work_title');
		}
		public function work_description() {
			return get_field('work_description');
		}
}

and then I duplicated the single.blade.php and I renamed single-work.blade.php

everything works well, but I am having problems to get the repeater field “work_block” works.
i tried to follow other topics in the forum, but without success.

could please somebody help me to understand how i can pull the repeater data using Controller?
I’m stuck here since 2 days :frowning:

Thanks in advance!

Your posted code does not show anything that would attempt to access that field.

  1. Please post the code that attempts to access the work_block field.
  2. How is it not working?
  3. What problems are you having, precisely?
  4. Are you seeing errors?
    • If so, what are they?
1 Like

ALSO
I end up with not using controller or data filter because I use a lot of ACF option page variables.
Data Filter would not pass ACF option page variables to post type page(like normal POST). And Controller, I feel it required too much lines that not simple as data filter.

My solution is just use BLADE php, and for each set of variable I put them to related parts or templates.

Example:

home-page.blade.php

@php
  $var = get_field('var');
@endphp

** if you use repeater with get_sub_field, you need to put the above lines inside of the @whille loop.

<p>{{ $var }}</p>
or
<p>{!! $var !!}</p>

Sometime if your field content has html markup code, you may just do normal {{ the_field('var') }} because this way would not output a blank format, like: TEST<sup>*</sup>.

Hope that helps!

the easiest thing to do is use Controller’s Advanced Custom Fields module:

// app/controllers/single-work.php
<?php

namespace App;

use Sober\Controller\Controller;

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

then in your blade template, something like:

@foreach($work_block as $block)
{{ $block->type }}
{{ $block->image }}
// etc.
@endforeach
3 Likes

@runofthemill thanks for this advice, I tried but I received these errors:

( ! ) Notice: Undefined variable: work_block in /srv/www/mywebsite.com/current/web/app/uploads/cache/17d866b6b69e432588a3fc8f15906ee88c17b3f4.php on line 5
( ! ) Warning: Invalid argument supplied for foreach() in /srv/www/mywebsite.com/current/web/app/uploads/cache/17d866b6b69e432588a3fc8f15906ee88c17b3f4.php on line 5 

this is the code in my blade template

@foreach($work_block as $block)
  {{ $block->video }}
@endforeach

I tried also

@foreach($work_block as $block)
  {{ $block->work_video }}
@endforeach

am i missing something?
any thoughts would be appreciated.
thanks

@runofthemill it was my fault, I didn’t run composer require soberwp/controller:2.0.1 as the instructions say.

these are the steps that I did:

  • run composer require soberwp/controller:2.0.1
  • in the functions.php I added this code
    add_filter(‘sober/controller/namespace’, function () {
    return ‘Data’;
    });

after that I received some errors, and I was not able to see the wp-admin, so I manually changed all the files in the composer folder from namespace App; to namespace App\Controllers;
Was I suppose to do this or not?

after that, I can finally see the wp-admin, but I still this error

because I manually changed a lot of files and I am still receiving errors, I am wondering if I followed the correct steps or I went in a blind alley

thanks in advance

You don’t need to add that snippet to to functions.php - unless you’re intentionally changing the namespace, which you likely don’t need to do here.

If you’re referring to the files in /vendor/soberwp/controller/ then yeah, not the right steps :stuck_out_tongue_winking_eye: Either switch those back, or simply delete that folder and run composer update to get a fresh, unaltered version.

It sounds like either your variable isn’t $work_block or the protected $acf = true; statement isn’t in the right place. In your template, where the @foreach loop is, temporarily replace that with @debug - that will output all the variables on the front end, similar to the php function var_dump(). You should see something like the following:

In your case, you’d be interested in the section that looks something like this:

SingleWork
* Data
  * work_block [array] // or possibly [object] 
  // the above key should be here if protected $acf = true is set up correctly

If you’re not seeing that, let me know. If you are, whatever that value under Data that corresponds to the ACF field is, just reference that as the variable in your @foreach loop.

1 Like

hi @runofthemill thanks again.

these are the steps that I followed:

  • run composer require soberwp/controller:2.0.1
  • change namespace App; to namespace App\Controllers; in /app/controllers/app.php and /app/controllers/front-page.php, and in the file that I created /app/controllers/single-work.php
  • rename app.php to App.php and front-page.php to FrontPage.php, and my file SingleWork.php

this was the only way to remove the errors that I had, because of the PSR-4 PSR-4: Autoloader - PHP-FIG (at least this is what I understood, reading other topics)

are you 100% sure that I don’t have to change namespace App; to namespace App\Controllers; in the files inside the /app/controllers/?

infact now I can see the @debug info
10

I ended up to switch to the ACF flexible content instead of the repeater and use a sort of page-builder like this topic Anybody using any page builders with sage? - #8 by nathobson

thanks again

i was supposed to said " I manually changed all the files in the /app/controllers/ folder " not the composer folder of course :slight_smile: (sorry about this)

Ah yeah you’re fine then :slight_smile:

This should work now, assuming the sub field is named work_video:

@foreach($work_block as $block)
  {{ $block->work_video }}
@endforeach