Support

Account

Home Forums Add-ons Flexible Content Field Merge/combine two flexible content fields into one Reply To: Merge/combine two flexible content fields into one

  • 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.