Development

Duplicating Sage 9’s ACF Return in Sage 10 with ACF-Composer

Nick Makris
Role:
Engineer
Jul 28, 2025

When working within roots.io’s Sage 9 theme, within the Controller files, there was a Class variable labeled as $acf. This variable allowed you to pass an array of ACF field names, which would allow it to be available to the blade template files automatically. When first working with Sage 10, back in late 2023, it was noticed that when working with the acf-composer package from log1x, that there wasn’t a lot of that same functionality in Sage 10 that was in Sage 9.

One of the biggest benefits from the Sage 9 was that all the ACF data, including the nested data, returned as objects instead of arrays. This allows for a lot more cleaner coding within the blade templates. However, while this alone doesn’t warrant enough to put in the time and effort just to have nested data return as an object, another piece of providing cleaner code, is being able to manipulate the data before it’s sent to the template.

AcfNestedFields

This is where AcfNestedFields comes into place. This class created to handle an array of fields combines the functionality of Sage 9’s protected $acf = [] in with the ability to return a Illuminate\\Fluent\\Collection of the fetched data, so that you can mutate the data before it’s sent to the template. Going back to our earlier data, we can pass through Class __construct’s $data array a field labeled subtitle and one labeled slides.

This subtitle field is a standard text field from ACF, and slides is a repeater field, which could have one to many internal fields.

Now with the repeater, you do not need to actually define them, as ACF already returns all their data for you, and the Class will format them appropriately.

You have a post object that you want to showcase in a card for the front end. However, that post has some ACF fields that isn’t inherently visible when you return a WP_Post. Within the blade template, you can use code to fetch the fields using get_field() for that data, however, when wanting to minimize how much PHP code you’re putting directly into your template, data mutation can be done directly in the ACF-Composer Class.

In this example, we are passing the field names subtitle, and slide_images to AcfNestedFields and call to the getFields() method, which will loop through each array item and call to get_field() as necessary. Now, while we could just do return ['subtitle' => get_field('subtitle')] in the with() method, when you have a lot of field data to return, it starts to get a bit cluttered.

This, with the aforementioned nested fields like repeaters, flexible field groups, links, and groups, the Class will iterate through them and set a key => value return for use on the front end no matter how deep the nesting goes.

Below you will find a full code snippet of what is outlined.

No items found.