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

Pass the block name to the render callback #4671

Closed
johnbillion opened this issue Jan 25, 2018 · 7 comments · Fixed by #21467
Closed

Pass the block name to the render callback #4671

johnbillion opened this issue Jan 25, 2018 · 7 comments · Fixed by #21467
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Enhancement A suggestion for improvement.

Comments

@johnbillion
Copy link
Member

Issue Overview

The block name is not passed as a parameter to the render_callback callback function. This means it's not possible to use a generic callback which performs routing to render dynamic blocks (for example, a generic handler which routes block rendering to theme template parts).

Expected Behavior

The block name is passed as a parameter to the render_callback callback function.

Current Behavior

Just the block attributes and content are passed.

@jeffpaul jeffpaul added [Type] Enhancement A suggestion for improvement. [Status] In Progress Tracking issues with work in progress labels Jan 26, 2018
@mtias mtias added the [Feature] Block API API that allows to express the block paradigm. label Jul 9, 2018
@mtias mtias mentioned this issue Aug 1, 2018
16 tasks
@benlk
Copy link
Contributor

benlk commented Oct 27, 2018

There are a number of "convert your shortcode to a block" tutorials that use the shortcode callback as the render_callback function, which is conveniently possible because the two callbacks share function signatures.

  • shortcode_callback( $attributes, $content, $shortcode_tag );
    • $attributes: an array of attributes
    • $content: the content wrapped in the shortcode tag
    • the entire shortcode tag
  • render_callback( $attributes, $content );

So there's three easy solutions:

  1. Pass the block name as a reserved array key in $attributes
  2. Pass the block comment-plus-contents as the third parameter of the render callback
  3. Pass the block name as the third parameter of the render callback

@youknowriad youknowriad removed the [Status] In Progress Tracking issues with work in progress label Apr 3, 2019
@carnilino
Copy link

carnilino commented Jul 28, 2019

You can pass block name via attributes using filter in php's render_block function.

function pass_block_name_to_render( $block, $source_block ) {
	$block['attrs']['_name'] = $block['blockName'];
	return $block;
}

add_action( 'render_block_data', 'pass_block_name_to_render', 10, 2 );

You can then access it in the render callback like this: $attributes['_name']

@aduth
Copy link
Member

aduth commented Apr 14, 2020

A possible solution to this is being explored at #21467 (comment) , where render_callback would be provided an instance of a block class containing all properties relevant to that block (including name), while still retaining backwards-compatibility by having the class implement ArrayAccess so that array access is treated as it behaves today with retrieving attributes of the block.

These would both work, and effectively behave the same:

register_block_type( 'my-plugin/favorite-color', [
    'render_callback' => function( $block_attributes ) {
        return 'Favorite color is: ' . esc_html( $block_attributes['color'] );
    }
] );
register_block_type( 'my-plugin/favorite-color', [
    'render_callback' => function( $block ) {
        return 'Favorite color is: ' . esc_html( $block->attributes['color'] );
    }
] );

It's still an early iteration, but feedback is welcome.

@aduth
Copy link
Member

aduth commented Apr 17, 2020

This is now possible as of the changes merged in #21467.

See documentation: https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/tutorials/block-tutorial/creating-dynamic-blocks.md

Example:

register_block_type( 'my-plugin/favorite-color', [
    'render_callback' => function( $block ) {
        return 'Block name: ' . esc_html( $block->name );
    }
] );

@vena
Copy link

vena commented Jun 15, 2021

is there a current method now that the render_callback only receives an attributes array? $block->name only throws an error.

@bph
Copy link
Contributor

bph commented Jun 15, 2021

The above documentation about creating dynamic blocks has moved to https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/block-tutorial/creating-dynamic-blocks.md

@vena
Copy link

vena commented Jun 15, 2021

Thank you, I had found the new location earlier... but the problem is the render_callback no longer appears to receive a WP_Block instance which was relevant to resolving this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants