Home › Forums › Add-ons › Flexible Content Field › Merge/combine two flexible content fields into one
I have a need for a specific behaviour.
Let’s say that I have Field Group called “Layouts” which contains only 1 field “flexible content” with name “default_layouts”.
Then I have another separate Field Group “More Layouts”. In this I create new flexible content field “additional_layouts”. And I disable this field (for it to now show up anywhere).
And then via (php) code I place “additional_layouts” to “default_layouts” as user opens edit post page in admin for example.
This “additional_layouts” won’t be seen in local json (ideally) of “default_layouts” – I save all ACF to local json (just to be sure).
Just loading as new layouts in “default_layouts” when editing a page/post or anything else.
I kinda did that, BUT it’s saving to JSON … which should be preventable?
Code for other:
add_filter( 'acf/load_field/name=blocks_content', array( $this, 'add_more_layouts' ), 10, 1 );
public function add_more_layouts( $field ) {
$add_layouts = array();
$more_layouts = acf_get_field( 'more_layouts' );
if( ! empty( $more_layouts[ 'layouts' ] ) ) {
$add_layouts = array_merge( $add_layouts, $more_layouts[ 'layouts' ] );
}
if( ! empty( $add_layouts ) ) {
$field[ 'layouts' ] = array_merge( $field[ 'layouts' ], $add_layouts );
}
return $field;
}
Well, for anyone wondering, I found a “solution”.
public function add_more_layouts( $field ) {
$add_layouts = array();
$default_layouts = acf_get_field( 'default_layouts' );
if( ! empty( $default_layouts[ 'layouts' ] ) ) {
$add_layouts = $this->merge_blocks_correctly( $add_layouts, $default_layouts[ 'layouts' ] );
}
$user_layouts = acf_get_field( 'user_layouts' );
if( ! empty( $user_layouts[ 'layouts' ] ) ) {
$add_layouts = $this->merge_blocks_correctly( $add_layouts, $user_layouts[ 'layouts' ] );
}
$global_layouts = acf_get_field( 'global_layouts' );
if( ! empty( $global_layouts[ 'layouts' ] ) ) {
$add_layouts = $this->merge_blocks_correctly( $add_layouts, $global_layouts[ 'layouts' ] );
}
$field[ 'layouts' ] = $add_layouts;
return $field;
}
public function merge_blocks_correctly( $layouts_start, $layouts_new ) {
foreach( (array) $layouts_new as $layout_key => $layout_data ) {
if( array_key_exists( $layout_key, $layouts_start ) ) {
unset( $layouts_start[ $layout_key ] );
}
}
$merge = array_merge( $layouts_start, $layouts_new );
return $merge;
}
How it works:
1) I get all default layouts from EMPTY (except first dummy layout) Flexible Content field
2) Then I proceed to load “user” layouts (another Flexible Content field)
3) These I merge BUT remove anything that was previously setup (duplicates)
4) I add more layouts (repetitive point 2 and 3)
5) Overwrite (return) layouts from EMPTY Flexible Content. Each is in seperate Field Group
This way I can modify multiple Flexible Fields separately and they load to the “empty” one.
You must be logged in to reply to this topic.
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.