• Resolved yepecece

    (@yepecece)


    Hi,

    I have a block (which can only appear once) with an image field with the “Featured thumbnail” option checked, but for some reason, the featured image is not saved.

    My CPT has the ‘thumbnail’ feature activated.

    Is this a bug related to the fact the the field is inside a block?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Yes it’s normal, inside a Block Type we can’t get the information of the current Post ID as usual, Therefore we cannot set the image as featured thumbnail correctly. This is due to how the ACF Block Type feature & Gutenberg work.

    I’ll see if I can find a workaround. In the meantime, I advise you to setup a normal Field Group with an Image field to set a Post Thumbnail.

    Sorry for the inconvenience.

    Regards.

    Thread Starter yepecece

    (@yepecece)

    Thanks for the info.

    Would it be possible to use some kind of action such as ‘acf/save_post’ in combination with parse_blocks() in order to save the info into the post thumbnail?

    My block is a Header block and as it is already setup in my theme I don’t want to have to modify all my pages.

    Thanks.

    PS: I know this is more an ACF question, but do you know if it is possible to show a metabox with ACF fields at the very top above gutenberg? Right now it is only possible on the side bar or on the bottom below Gutenberg.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Yes, that’s a good idea. I’m not a fan of this solution as it’s not “field based”, but it will work in your case. Here is a code example:

    add_action('acf/save_post', 'my_acf_blocks_save');
    function my_acf_blocks_save($post_id){
        
        // Bail early if not a Post
        if(!is_numeric($post_id))
            return;
        
        // Parse Blocks
        $blocks = parse_blocks(get_the_content($post_id));
        
        /*
         * Array(
         *      [0] => Array(
         *          [blockName] => acf/my-block-type
         *
         *          [attrs] => Array(
         *              [id] => block_5f3575c1bcbff
         *              [name] => acf/my-block-type
         *              [data] => Array(
         *                  [image] => 189
         *                  [_image] => field_5f35759ff724d
         *              )
         *              [mode] => preview
         *          )
         *
         *          [innerBlocks] => Array()
         *          [innerHTML] =>
         *          [innerContent] => Array()
         *      )
         * )
         */
        
        $thumbnail_id = false;
        
        foreach($blocks as $block){
            
            // Target specific ACF Block Type
            if($block['blockName'] !== 'acf/my-block-type')
                continue;
            
            // Check the field
            if(!isset($block['attrs']['data']['image']) || empty($block['attrs']['data']['image']))
                continue;
        
            // Retrieve Thumbnail ID
            $thumbnail_id = $block['attrs']['data']['image'];
            
            // Stop foreach
            break;
            
        }
        
        // Set Post Thumbnail ID
        update_post_meta($post_id, '_thumbnail_id', $thumbnail_id);
        
    }
    

    I’ll look for a more elegant solution (field based, instead of being based on page save) for ACF Extended in the future.

    Concerning your question about metabox placement, unfortunately Gutenberg doesn’t allow it. Maybe this will come in a future ACF update.

    Hope it helps!

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Image field in ACF Block as Featured thumbnail for CPT’ is closed to new replies.