Home › Forums › Add-ons › Flexible Content Field › Programmatically adding layouts to a flexible content field
Hi All
I have seen similar topics on the forums to do what I am trying to do but nothing quite right so I am hoping you guys can help me out.
I am developing a base theme that we can use as a basis for our projects that relies on having re-usable layouts that can be added to an ACF field group and used throughout the sites.
What I want to be able to do is select one of my layouts from a dropdown, then select the field group, and field within that field group that I want to install that layout to, it will then grab all the information that I have stored in an array for that layout, and add it as an available layout of the ACF field. I hope that makes sense!
I can’t find any documentation on how to actually programatically add a layout to an existing field in ACF. I did find something on another topic that mentioned something like this:
$field_data = acf_get_field($field);
$key = uniqid();
$field_data['layouts'][$key] = array(
'display' => 'block',
'key' => $key,
'label' => 'Test',
'max' => '',
'min' => '',
'name' => 'test',
'sub_fields' => array()
);
acf_remove_local_field($field);
acf_add_local_field($field_data);
However this doesn’t seem to do anything. I suspect this is something to do with the local_field part? It is worth noting that these fields are not adding using PHP in the functions file, they are added using the admin interface then cached into the acf-json files.
Any guidance would be greatly appreciated!!
Many thanks
David
I am still working on this and am slowly progressing forward, I have now managed to get it to add the layout using the following code:
$field_data = acf_get_field($field);
$key = uniqid();
$field_data['layouts'][$key] = array(
'display' => 'block',
'key' => $key,
'label' => 'Test',
'max' => '',
'min' => '',
'name' => 'test',
'sub_fields' => $sub_fields
);
acf_update_field($field_data);
The problem is, no matter what I pass as the sub_fields, they get stripped out by the update_field method on the flexible content plugin before the save is called on line 1334:
function update_field( $field ) {
// loop
if( !empty($field['layouts']) ) {
foreach( $field['layouts'] as &$layout ) {
unset($layout['sub_fields']);
}
}
// return
return $field;
}
I am not quite sure why this would be? Is there a way that I can re-add the sub_fields to the layout afterwards?
A note to anyone else that stumbles upon this, using acf_update_field does not refresh the cached version in the acf-json folder so you need to remove those files if you want to use this method.
Any input would be greatly appreciated!!
All the best
David
The topic ‘Programmatically adding layouts to a flexible content field’ 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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.