I’m working on building a Flexible Layout field group that works like a “Card Grid”. It’s just a Select field to pick a Card Style, followed by a repeater where each row is a clone of another field group that describes that card’s attributes (image, heading, url, whatever). Those clones are set do display as a group so that I can use the conditional logic to only display the clone associated with the selected style.
Here’s the JSON describing that field group. In this case I have two other Card Layout field groups being cloned: Post Card and Team Member Card.
This totally works how I want it to, but ultimately my goal is to be able to programmatically generate the Card Style options and Card Data clones based on the Card Layout field groups in the system.
The Card Style select was easy. First I wrote a function to get my card layouts, then I feed that into the choices attribute for the select field on acf/load_field/key=field_card_grid_style. This comes out exactly the same as when I do it by manually adding options to the Card Style select field.
The problem comes in when I try to generate the clones. My (seemingly misguided) approach here is to use acf_get_fields() to get the field object for my repeater, then I make a new object which is a clone of that but with an updated sub_fields attribute, then I pass that object into acf_update_field(). Since I only need to update the repeater when a new layout is added, I do this using the ‘acf/update_field_group’ action instead of as a filter. Here’s the code:
This all seems correct to me in theory. To run the test I manually removed the Team Member Card clone form the Card Data repeater and then saved the field group. This triggers the acf/update_field_group action, and I get the log below. Everything *looks* right to me in here, and I don’t get any error messages, but the field doesn’t actually get the new clone; it just silently fails. I know that SUB fields can sometimes work differently than regular fields, so my gut tells me I am not taking that into account properly, but I am at a loss here. Any ideas?
Also for more info I’m including the log messages I dump out at the close of the action.
Oh my approach here was SO WRONG!
Somehow I’ve gotten along just fine for so long without knowing about acf_add_local_field_group()! Instead of juggling json files and synchronization, all I had to do was rewrite this to build the field groups from code in the first place. Once that function clicked, this all fell into place.
The topic ‘Programmatically add clone fields to a repeater’s subfields?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!