• Hi,

    I need some help. I’m just starting out customizing using gutenberg, so perhaps I just have missed something.

    I’m trying to add a call to action to be shown show in all posts and have created a reusable block for that. It works fine, but has to manually be added, what can be forgotten.

    I found out how block templates can be used to prefill new posts with blocks and that is very useful too.

    So now I could add the call to action block as a normal block and try to prefill it, or I can find out if a template can be used to add a reusable block too.

    Optimal for my user case would be to add a standard block, with fixed settings and content. Using a reusable block seems better equipped to discourage editing the block, but I haven’t found any example of how such a block can be added to a block template.

    And trying to prefill the non-reusable version of the block, I also run into trouble, and don’t know how to prevent or discourage users from changing the settings or content in the editor. So my preference would be using a reusable block.

    My questions:
    – Can anyone tell me if and how a reusable block can be used in a block template?

    The call 2 action is added by the genesis block plugin and is basically a div that contains a h2 title, followed by a text area ( a div on the frontend) that can contain one or more paragraphs and a button. I was able to prefill all settings and content, except for the ctaText that has paragraphs as children.

    When I examine the block in console using:
    wp.data.select( 'core/block-editor' ).getSelectedBlock().attributes;

    It shows me that ctaText is an array that contains an object { type: 'p', props: {...}}. The props open up as an object which contain children which contain the text.

    Anyone know a way to reach such a child to prefill it?

    If none of this works, I will probably just need to filter the_content and add some html programmatically, but I rather learn how to use gutenberg for such things.

    The template documentation is a start, but not yet an answer for all questions. The obvious question is how to find out in a given case how to address the settings of a block and what the arrays mean in case of the nested blocks. I see empty arrays being passed there, but don’t know why they are there and what other use they have. I’ld like to be able to find that kind of info.

    Any help is greatly appreciated.

    Hans

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Daniel Richards

    (@talldanwp)

    Can anyone tell me if and how a reusable block can be used in a block template?

    You should be able to use a reusable block in a template, but you’d only be able to reference an existing reusable block that you’ve created.

    The steps would be:
    1. Create the reusable block.
    2. Note the id of the reusable block. One way to do this is to go to the ‘Manage all reusable blocks’ page (https://localhost:8888/wp-admin/edit.php?post_type=wp_block), edit the block and grab the post id from the URL. The other is to add the reusable block to a post and switch to the code editor where you’ll be able to see the id next to where it says ‘ref’.
    3. In your template add the following to reference your reusable block (if your reusable block id were 123):
    array( 'core/block', array( 'ref' => 123 ) )

    Using the custom post type example from the docs that might look a bit like:

    function myplugin_register_book_post_type() {
        $args = array(
            'public' => true,
            'label'  => 'Books',
            'show_in_rest' => true,
            'template' => array(
                array( 'core/block', array(
                    'ref' => 123,
                ) ),
                array( 'core/heading', array(
                    'placeholder' => 'Add Author...',
                ) ),
                array( 'core/paragraph', array(
                    'placeholder' => 'Add Description...',
                ) ),
            ),
        );
        register_post_type( 'book', $args );
    }
    add_action( 'init', 'myplugin_register_book_post_type' );
    Thread Starter Hans Schuijff

    (@hanswitteprins)

    @talldanwp Hi Daniel,

    I love it, it your solution works. I didn’t know how to address an existing block by post_id, so now I know.

    In testing this, I have noticed another problem that makes using templates a bit risky.

    I had a non-core block in my template. It was part of a plugin and when I didn’t have that plugin activated on my system, the response was just an empty blank page with no further messaging.

    Only after I went to console, I understood the problem (a none existing block type), but that is only for developers to find and understand. And since I find that often the console has a lot of messaging concerning react and Gutenberg anyway, it might not be that noticeable when not looking for it.

    Gutenberg should perhaps just be able to skip/ignore non-existing blocktypes that are mentioned in the blokc-template array instead of just crashing the entire page, or at least it should offer a response that is clear for a user and doesn’t rely on using the browser’s go back key to return to return to WordPress.

    Is that an know issue?

    Again, thanks for the help. Great to know how I can use reusable blocks in templates. It makes the other challenge of prefilling the more complex blocktypes perhaps redundant.

    Regards,

    Hans

    Plugin Author Daniel Richards

    (@talldanwp)

    Gutenberg should perhaps just be able to skip/ignore non-existing blocktypes that are mentioned in the blokc-template array instead of just crashing the entire page, or at least it should offer a response that is clear for a user and doesn’t rely on using the browser’s go back key to return to return to WordPress.

    Is that an know issue?

    @hanswitteprins Yep, I’d expect the block to be considered ‘missing’ just like if it were added to a post and then the plugin was removed.

    I tested though, and you’re right, the template system is less flexible, I’ve made a bug report here to cover the issue:
    https://github.com/WordPress/gutenberg/issues/27731

    Thread Starter Hans Schuijff

    (@hanswitteprins)

    @talldanwp Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Block templates using Reusable blocks?’ is closed to new replies.