Just did more work on automating the acf fields from Controller to view.
The returned variables are based on acfs key values. You can then override any values by using the same key value in camel case in your Controller for more control.
<?php
namespace App\Controllers;
use Sober\Controller\Controller;
class Single extends Controller
{
protected $acf = true;
public function textField()
{
return 'Override automated field';
}
}
I’m trying to get ACF fields working and an encountering some issues.
I’m running the most recent Sage beta, and I can not figure out how to get the fields exposed as simple variables as I see everyone doing here.
Note that my image field is just returning the URL string.
Controller app/controllers/App.php:
namespace App;
use Sober\Controller\Controller;
class App extends Controller
{
public function heroImg()
{
$image = get_field('hero_image');
return $image;
}
}
Did you read the whole thread? You absolutely don’t need a separate function for each variable. Just build an array/object of your data to return within your method.
Did you read the whole thread? You absolutely don’t need a separate function for each variable. Just build an array/object of your data to return within your method.
Yes. My comment “been there, done that” was about snake casing of the method names BTW.
Whether you package all the variables into an array, object, or even a collection, wasn’t my point. On the Blade side, you have to do $object->related and $object->unrelated kind of thing or define separate functions for the unrelated.
Either way, when does it stop being a controller and actually be a transformer, or both. Just confusing, is all I am saying.
Similar to @olafghanizadeh’s post, I am trying to display the ACF Post Object on front-page.blade.php. I first tried to display using vanilla php (using ACF’s Post Object article) before attempting to use proper blade and controller syntax just to see if it works:
I’m doing exactly what you’re doing in several themes (I just double-checked my code) and it’s working. Are you sure your field is named featured_article ?
If you add print_r($p) right after your foreach does it give you info about the featured article?
I got it to work with the last code block I posted using the Relationship ACF Field Type and your tip to use the Basic loop method. However, the Post Object keeps displaying the Home page. That’s ok though, I can use the Relationship Field Type for the purpose of this page and I got it to work with controller/blade syntax as well. Thanks so much for the help!
Hi there, do you know when Controller 2.0.0 is going to be used by Sage as a default?
Are there any potential issues with updating this manually in order to get the most out of the ACF module you’ve so brilliantly created? As suggested in this thread: Setting up a controller for ACF fields
How will this be affected by future updates to Sage?
Easy way would be to use get_field and store the flexible field data into a variable and then foreach over it to access the data you need (in your controller).