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 as a parameter to the dynamic block render callback #4673

Closed
wants to merge 2 commits into from

Conversation

johnbillion
Copy link
Member

Description

See #4671. Implements the $name parameter for dynamic block callback functions.

How Has This Been Tested?

  • Testing via registering a dynamic block callback.
  • Unit tests have been updated to test the parameter is correctly passed.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

@aduth
Copy link
Member

aduth commented Jan 25, 2018

Noting that #4591 removes the $content argument from render_callback, which would conflict with changes proposed here.

Also seems like a strange requirement if the block name is known at the time of assigning a render callback. Admittedly to make this generic, the most obvious solution to me would require support for PHP closures (5.3+).

<?php

function my_plugin_generic_block_render( $block_name, $attributes ) {
	// ...
}

$block_names = [
	'my-plugin/foo',
	'my-plugin/bar',
	'my-plugin/qux',
];

foreach ( $block_names as $block_name ) {
	register_block_type( $block_name, [
		// ...

		'render_callback' => function() use ( $block_name ) {
			call_user_func_array(
				'my_plugin_generic_block_render',
				array_merge( [ $block_name ], func_get_args() )
			);
		}
	] );
}

(Or some equivalent pattern for function partial application)

@aduth
Copy link
Member

aduth commented Jan 25, 2018

Or alternatively, if we expect to need to know information about the block in the render callback, maybe it's enough to pass the entirety of the instance of the block to the render callback.

@aduth
Copy link
Member

aduth commented Jan 25, 2018

Per #4671, it's hard to provide judgment here without real-world use-cases, only "it might be useful if..."s

@johnbillion
Copy link
Member Author

the most obvious solution to me would require support for PHP closures

Yeah that's exactly how we're doing it at the moment.

@johnbillion
Copy link
Member Author

maybe it's enough to pass the entirety of the instance of the block to the render callback

That could be an option too.

@mtias mtias added [Feature] Block API API that allows to express the block paradigm. Framework Issues related to broader framework topics, especially as it relates to javascript labels Jan 26, 2018
@johnbillion johnbillion closed this May 7, 2018
@aduth aduth deleted the update/render-callback-name branch January 25, 2019 21:38
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. Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants