Home › Forums › Add-ons › Flexible Content Field › Programmatically adding layouts to a flexible content field › Reply To: Programmatically adding layouts to a flexible content field
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
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.