Support

Account

Home Forums Gutenberg Preview parm on acf_register_block_type callback fn

Solved

Preview parm on acf_register_block_type callback fn

    • sschat

    • January 9, 2020 at 11:41 pm

    Hi!
    I might be missing something but on the callback function for the “acf_register_block_type” there are four input parms. (taken from the examples)

    The third one is the “preview” parm.
    So we can use this to render different output to backend and frontend, right?

    But if i debug this “preview” parm, i get a complete array as result.
    Is this correct?
    Cause now “preview” is true due to this array, hence i get the wrong view on the frontend.

    Here some code and results of it:

    function fn_render_block_podcast( $block, $content = '', $is_preview = false, $post_id = 0, $a = '' ) {
    
    	debug('$block');
    	debug($block);
    	debug('$content');
    	debug($content);
    	debug('$is_preview');
    	debug($is_preview);
    	debug('$post_id');
    	debug($post_id);
    	debug('$a');
    	debug($a);
    
    	// Render the block.
    	\Timber::render( get_template_directory() . '/templates/ACF-blocks/' . strtolower( $block['title'] ) . '/' . strtolower( $block['title'] ) . '.twig', $context );
    
    }

    Results:

    $block
    
    Array
    (
        [name] => acf/podcast
        [title] => Podcast
        [description] => Kennisnet ACF block: Podcast
        [category] => kennisnet_acf
        [icon] => Array
            (
                [background] => #44A0A1
                [foreground] => #fff
                [src] => format-audio
            )
    
        [mode] => auto
        [align] => wide
        [keywords] => Array
            (
                [0] => kennisnet
                [1] => acf
            )
    
        [supports] => Array
            (
                [align] => 1
                [html] => 
                [mode] => 1
            )
    
        [post_types] => Array
            (
                [0] => article
                [1] => page
                [2] => meeting
            )
    
        [render_template] => 
        [render_callback] => fn_render_block_podcast
        [enqueue_style] => 
        [enqueue_script] => 
        [enqueue_assets] => Closure Object
            (
                [this] => Kennisnet\ACF\ACF_register_block Object
                    (
                        [blocks_dir] => /Users/sanderschat/Websites/- KENNISNET/wp-app-kennisnet/web/app/themes/kennisnet/templates/ACF-blocks/
                        [block_dir] => /Users/sanderschat/Websites/- KENNISNET/wp-app-kennisnet/web/app/themes/kennisnet/templates/ACF-blocks/quote
                        [blocks_url] => http://kennisnet.local:8888/app/themes/kennisnet/templates/ACF-blocks/
                        [block_file] => /Users/sanderschat/Websites/- KENNISNET/wp-app-kennisnet/web/app/themes/kennisnet/templates/ACF-blocks/quote/quote
                        [block_name] => quote
                        [block_url] => http://kennisnet.local:8888/app/themes/kennisnet/templates/ACF-blocks/quote/quote
                    )
    
            )
    
        [id] => block_5dd3bc0a92092
        [data] => Array
            (
                [podcast_link] => //html5-player.libsyn.com/embed/episode/id/5184654/theme/standard/
                [_podcast_link] => field_5daefffc46dca
            )
    
    )
    
    $content
    
    $is_preview
    
    Array
    (
        [blockName] => acf/podcast
        [attrs] => Array
            (
                [id] => block_5dd3bc0a92092
                [name] => acf/podcast
                [data] => Array
                    (
                        [podcast_link] => //html5-player.libsyn.com/embed/episode/id/5184654/theme/standard/
                        [_podcast_link] => field_5daefffc46dca
                    )
    
                [align] => wide
                [mode] => auto
            )
    
        [innerBlocks] => Array
            (
            )
    
        [innerHTML] => 
        [innerContent] => Array
            (
            )
    
    )
    
    $post_id
    
    1560
    
    $a

    I added the $a parm just for testing. Cause i thought i might miss a parm.

    Any thoughts on this? A bug? OR should i call it differently?

    • sschat

    • January 21, 2020 at 8:33 pm

    So, to “fix” my own issue here:

    The preview parameter is not correctly passed on by ACF, so i could not use that.
    I created my own preview paramter just by checking if we are in the adminbackend or not.

    So the renderblock is as followed:

    function fn_render_block_podcast( $block ) {
    
    	// are we in the backend?
    	$is_preview = is_admin() ? true : false;
    
    	$context = [];
    	// Store field values
    	$context['podcast_link'] = get_field( 'podcast_link' );
    	$context['is_preview']   = $is_preview;
    
    	// Render the block.
    	\Timber::render( get_template_directory() . '/templates/ACF-blocks/' . strtolower( $block['title'] ) . '/' . strtolower( $block['title'] ) . '.twig', $context );
    
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.