I’d even looked in the debugger to check the namespace but I think I’m panicking too much to think properly at the moment. My understanding is very tentative but I’m getting there. Your help has been invaluable.
I did reply to your other post on this but this is more helpful as you have provided some code to look over. The problem here is that you are not looping over any data; there is no context for get_sub_field; how would it know which field to call it against or which row to display?
I like to follow the principal that all logic takes place in the controller and not the view and so I also try to only pass the data I’m actually to need for the view and in its final format.
This is actually more like a repeater, as with a flexible content field you’ll likely want to different logic based on the particular layout but the concept would remain similar.
You would then loop through the resulting object in your view.
Thanks for the quick reply. I have noticed that you contribute a lot to the forms and am very appreciative.
I am trying to have a controller for each flexible layout.
Example: I have views/flexible-fields/content.blade.php. I want a unique controller that manages all the custom fields for content.blade.php in app/controllers/content.php.
Here is the controller:
app/controllers/content.php
How come I cannot access the object from app/controllers/content.php on the view? When I place the logic from app/controllers/content.php to app/controllers/front-page.php the blade then can access it?
You seem to be making the assumption that Controllers are linked to Blades—i.e. that content from Controller.php will be automatically piped to controller.blade.php. This is not the case. Controllers are linked to the WordPress Template Hierarchy—i.e. content from the Controller in Page.php will appear at locations where WordPress would load the Single Page template. There is no way (that I’m aware of) to target individual Blades from Controllers.
The way both Controller and Sage’s own routing do this is by hooking into the values that pass through the body_class filter, and loading the appropriate content. You can determine what controller should be used for your page by looking at the value of the class attribute on your page’s <body>. For instance, take a look at this page:
If I wanted to target this with a controller, I could use a Controller called Single or LocationTemplateDefault, because of these classes:
Controller names need to conform to PSR-4 naming standards, which is why it’s LocationTemplateDefault, not location-template-default.
If your goal is to encapsulate functionality you intend to use on several controllers, you might try using traits as described in the Controller documentation. They won’t allow you to target specific Blades, but they will allow you to re-use content across Controllers.
Hello I really like the idea of creating a controller for acf fields but I have one problem which I can not solve. I have group field for section, the group contains section title and content (content is WYSIWYG editor field). When I want to display value from content field which is WYSIWYG editor I see HTML tags as normal string text.
Example: <p>Lorem <strong>ipsum</strong> dolor sit amet</p>
My controller function looks like this:
public function content() {
return (object) array(
'section' => get_field('section'),
);
}
And I displayed fields in php like this: <h3>{{$content->section['title']}}</h3> <div>{{$content->section['text']}}</div>
My question is: How to display formatted fields from WYSIWYG editor using controllers?
The only way is to manually display the field using the_field() function, but is there any possibility to create controller for that?
I am starting a new project now and I don’t want to mess up everything from the beginning, so I do a recap of the steps that I followed (I hope that this can help somebody in the future)
If I made some big mistakes or something that I should avoid to do, it would be great if you can let me know and save my life
I’d really really love to be able to work with flexible content the correct way using a controller, views and partials. I’ve attempted to get flexible content working in this way by lots of trial and error and research but just can’t quite figure it out.
I’m asking if anyone here would be willing to break down the following code example into how you would use this flexible content to achieve the results. I’m sure this would be super helpful to lots of others as well. I’ve been unable to find anyone really breaking down the approach in a way that’s easy to understand.
This code uses two flexible content sections. One full width-one column section and a one full width-two column section. That’s pretty obvious and not really the point but I’m hoping someone will be willing to break this down.