Make WordPress Core

Opened 10 months ago

#58278 new enhancement

[Query Loop][Post Template][Custom dynamic block] Impossible to pass query loop context to a custom dynamic block as the post template context is hard coded.

Reported by: jdmweb's profile jdmweb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.2
Component: Editor Keywords: dev-feedback
Focuses: Cc:



Context :

I'm writing a query loop variation, with post templates, then a custom dynamic block.

Problem :

I was trying to get the query loop id inside the custom dynamic block and could not, even when specifying usesContext and providesContext block.json configuration for my dynamic bloc of the core/post-template one.

Observations :

After some investigation, I found that the post template uses the render_block_core_post_template function as render callback inside the post-template.php file

Line 93 of this file, we can see that the function hard codes the context that is then passed to the block :

                $block_content = (
                        new WP_Block(
                                        'postType' => get_post_type(),
                                        'postId'   => get_the_ID(),
                )->render( array( 'dynamic' => false ) );

There's no filter or mechanism allowing us to provide more context here which is quite blocking.

Suggestion :

We could do something like this instead :

                // Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
                // `render_callback` and ensure that no wrapper markup is included.
        $context = array(
            'postType' => get_post_type(),
            'postId'   => get_the_ID(),
        //Check if additional context is provided by ancestor blocks
        if (!empty($block->block_type->provides_context)) {
            foreach ($block->block_type->provides_context as $context_name) {
                if (array_key_exists($context_name, $block->context)) {
                    $context[$context_name] = $block->context[$context_name];

                $block_content = (
                        new WP_Block(
                )->render( array( 'dynamic' => false ) );

With this code, I was able to specify and get the queryId to my custom block.

Steps to reproduce :

  • Create a query loop variation
  • In the variation, set the innerblocks setting to use a custom dynamic block.
  • In your custom block block.json file, we're trying to use the queryId as the context.
  • As it's not working, I've also specified that I wanted the core/post-template block to provide the queryId via providesContext.
  • Realize the core/post-template block does not provide any context besides the hard coded one mentioned above.

Expected output :

If we specify that the core/post-template block should specify a context, it should be taken into account alongside the default values that are postType and postId

Thank you for your work.

Change History (0)

Note: See TracTickets for help on using tickets.