• Hi everyone,
    I am wondering about the right way to handle dynamic datas in a block requiring innerblocks.

    I recently encountered an issue while trying to migrate dynamic post title to static core/heading. Part of the problematic happened because I was both using a dynamic block behaviour (render.php rendering php dynamic values such as post title), but also using a static save function to save innerBlocks.

    Block.json

    {
    ...
    "render": "file:./render.php",
    ...
    }


    save function
    save: props => { return <InnerBlocks.Content /> }

    Render.php

    <section <?php echo get_block_wrapper_attributes(['class' => 'my-block-name']) ?>>
    
       <h1><?php echo get_the_title() ?></h1>
       <h2><?php echo $attributes['someCustomAttribute'] ?>
       // and imagine here you want to get some datas from another post, taxonomy, related-post-type
    
       <div class="my-block-name__content">
         <?php echo $content ?>
       </div>
    
    </section>


    This example works well, but cause problem when trying to migrate dynamic datas to static datas.

    Using custom attibutes inside the blocks would fix the migration problem, but then needs the user to update each post the block is used in ? (lest’s imagine there are 30,40,50 posts …)

    I have seen other examples (like here), and I was then wondering if this approach is ok or not ? Is there a clean(er) way to use both dynamic datas and static innerBlocks ?

Viewing 1 replies (of 1 total)
  • Hi there!

    To start, the way you are using <InnerBlocks> with a dynamic block is correct and the standard way of working with them in that context. You must have a save function that returns <InnerBlocks.Content/ > in order to have access to the inner block content inside the render

    Perhaps I am fully not understanding the use-case here, but I think that you would want to stay with a dynamic block as you’re accessing information from the database as opposed to data stored in attributes. If you wanted to add new attributes, you are correct that adding custom attributes would require an update to all instances of the block but, depending on what you are doing, you could use default attribute values and the migrate property of the deprecation object to set things up.

    I’m not sure if this is much help to you ?? Let me know if there are any more specifics and perhaps we can come up with an approach that would work for your case.

Viewing 1 replies (of 1 total)
  • The topic ‘Use innerBlocks in a gutenberg dynamic Block ?’ is closed to new replies.