Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Available block context does not flow to blocks inside the Post Template block #68608

Open
2 of 6 tasks
helgatheviking opened this issue Jan 10, 2025 · 0 comments
Open
2 of 6 tasks
Labels
[Block] Post Template Affects the Post Template Block [Type] Bug An existing feature does not function as intended

Comments

@helgatheviking
Copy link
Contributor

helgatheviking commented Jan 10, 2025

Description

I was trying to provide some context from the Query block down to the Post Title block (or any block inside the Post Template) and modified the suggestion described here.

But my custom context key was never available.

I tracked this down for a while and ultimately I think it is because of how the post template block is dynamically rendered. Specifically, it doesn't pass a 2nd parameter when instantiating the new WP_Block() for each post in the query's loop. So then in the WP_Block class there's no available context to look at when setting the new block's context.

Apologies if this should be reported to core somewhere.

At a minimum, if

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

Is changed to

$block_content = ( new WP_Block( $block_instance, $block-context ) )->render( array( 'dynamic' => false ) );

Then any context on the post template block will be available for consumption on the post title and other nested blocks.

Step-by-step reproduction instructions

  1. Activate below code snippet
  2. Insert a query block anywhere and save.
  3. view on front end. Each post title should be prefixed by "no tacos" (a very sad state, indeed)

Screenshots, screen recording, code snippet

add_filter( 'render_block_context', function( $context, $parsed_block ) {
	if ( 'core/query' === $parsed_block['blockName'] ) {
		$context['taco'] = 'we love tacos';
	}
	return $context;
}, 10, 2 );

add_filter( 'block_type_metadata',  function( $metadata ) {

	if ( ! isset( $metadata['name'] ) ) {
		return $metadata;
	}
	
	if ( 'core/query' === $metadata['name'] ) {
		$metadata['providesContext']            ??= [];
		$metadata['providesContext']['taco']    = 'taco';
	}

	if ( 'core/post-template' === $metadata['name'] ) { // Ideally, I shouldn't need this either.
		$metadata['usesContext']   ??= [];
		$metadata['usesContext'][] = 'taco';
	}

	if ( 'core/post-title' === $metadata['name'] ) {
		$metadata['usesContext']   ??= [];
		$metadata['usesContext'][] = 'taco';
	}

	return $metadata;
} );


add_action( 'render_block_core/post-title', function( $block_content, $block, $instance ) {
    
	if ( ! isset( $instance->context ) ) {
        return $block_content;
    }

    $context = $instance->context;
    $taco    = $context['taco']    ??= 'no tacos';

    return $taco . ' ' . $block_content;

}, 10, 3 );

Image

Environment info

WP 6.7.1
Twenty Twenty four

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Template Affects the Post Template Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

2 participants