Support

Account

Home Forums Front-end Issues align_content/alignContent in ACF 6.x

Solving

align_content/alignContent in ACF 6.x

  • I tried to create this as a support ticket, but after two days I’m guessing the team has its plate full with ACF 6.x bugs. So here it is for the forum
    ….

    I have a number of ACF based blocks using the matrix content align option.

    As described in the ACF 6.x upgrade notes this attribute will change from “align_content” to “alignContent”, but after updating (on dev, of course) it doesn’t exist as neither “align_content” nor “alignContent”. They are both simply empty when I get them from the template that renders the block.

    Even if “align_content” has a value for the block when checking it in the database, it looks like the the ACF rendering function removes it.

    If I edit the page I see “align_content” still exist as an attribute on the block in Gutenberg code editor, and if I switch back to preview and return to code “align_content” is replaced with “alignContent” – which is fine, I guess.

    But, this requires me to edit all pages for them to have the correct value in “alignContent”.

    These functions acf_get_block_back_compat_attribute_key_array and acf_add_back_compat_attributes in wp-content/plugins/advanced-custom-fields-pro/pro/blocks.php apparently should handle backwards compatibility for rendering, but the syntax isn’t logical and only changes something id there is a value in “alignContent” ….which there never will be for blocks made with versions prior to 6.0

    function acf_add_back_compat_attributes( $block ) {
    	foreach ( acf_get_block_back_compat_attribute_key_array() as $new => $old ) {
    		if ( isset( $block[ $new ] ) ) {
    			$block[ $old ] = $block[ $new ];
    		}
    	}
    
    	return $block;
    }

    As a very, very, very temporary solution I added a placeholder attribute before rendering the block, and I can use it in the template to make my own check for a legacy value

    function acf_legacy_attributes_missing( $parsed_block, $source_block, $parent_block ) {
    
    	$parsed_block['attrs']['align_content_acf_legacy']	= ( !empty( $parsed_block['attrs']['align_content'] ) 	? $parsed_block['attrs']['align_content']	: '' );
    	$parsed_block['attrs']['align_text_acf_legacy'] 	= ( !empty( $parsed_block['attrs']['align_text'] ) 	 	? $parsed_block['attrs']['align_text'] 		: '' );
    	$parsed_block['attrs']['id'] 						= ( !empty( $parsed_block['attrs']['id'] ) 	 		 	? $parsed_block['attrs']['id'] 				: 'block_id_' . uniqid() );
        return $parsed_block;
    }
    add_filter( "render_block_data", "acf_legacy_attributes_missing", 10, 3 );

    This adds the attribute align_content_acf_legacy, align_text_acf_legacy and ID holding the old values.

    So, my questions is – did I miss something in the upgrade notes or is this simply a ACF 6.x bug?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.