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:
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
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>.
// 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
@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
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 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.
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/?