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 parent block context during server side rendering #40714

Open
Hug0-Drelon opened this issue Apr 29, 2022 · 7 comments
Open

Pass parent block context during server side rendering #40714

Hug0-Drelon opened this issue Apr 29, 2022 · 7 comments
Labels
[Feature] Blocks Overall functionality of blocks Needs Dev Ready for, and needs developer efforts [Package] Server Side Render /packages/server-side-render REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.

Comments

@Hug0-Drelon
Copy link
Contributor

What problem does this address?

Currently, there is no way to use the <ServerSideRender /> component with the block context. Only the rendered block attributes are sent (see). It means it is not possible to render a custom block in the editor accordingly to its context (unlike core blocks for instance). This does not allow good WYSIWYG in my opinion.
I'm aware WordPress advises to render custom block using JavaScript. But with heavily dependent rendering on PHP (with all kind of data or complex logic) it can be hard to maintain.

What is your proposed solution?

  • <ServerSideRender /> could accept a new props (like parentContext) and pass it to apiFetch().
  • Change the way /wp/v2/block-rendered/ endpoint is registered and allow the new argument parentContext.
  • Since WP_REST_Block_Renderer_Controller::get_item() is using render_block() to generate the response and render_block() is not able to handle parent context, it may required to change the behavior of render_block(). Maybe with a new optional parameter ?

I'd be glad to help with PRs, the only thing is I don't know how to split the code (i.e. one PR on Gutenberg for the <ServerSideRender /> and an other on WordPress for get_item() and render_block() ?).

@gziolo gziolo added [Package] Server Side Render /packages/server-side-render [Feature] Blocks Overall functionality of blocks Needs Dev Ready for, and needs developer efforts REST API Interaction Related to REST API labels Apr 30, 2022
@gziolo
Copy link
Member

gziolo commented May 5, 2022

I'd be glad to help with PRs, the only thing is I don't know how to split the code (i.e. one PR on Gutenberg for the <ServerSideRender /> and an other on WordPress for get_item() and render_block() ?).

Thank you for raising the issue and offering your help. I think you should be able to implement all the necessary changes in one PR inside the Gutenberg plugin. For REST API call you should be able to use existing hooks to pass additional params. The worst case scenario would be overridden the existing endpoint by reusing the current one with get_item() reimplemented. I don’t know what has to change for render_block() but there are also WP hooks available.

@gziolo gziolo mentioned this issue May 23, 2022
58 tasks
@EmranAhmed
Copy link

It would be great if we can pass parent block context during server side rendering.

@tanvirulhaque
Copy link

I have the same issue, can't access context during server side rendering from the block editor. Do you guys have a plan to add the built-in support?

@gziolo
Copy link
Member

gziolo commented May 22, 2023

Yes, we definitely want to see this issue resolved!

It looks like #34882 covers the same issue.

@sovetski
Copy link

Same here

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Sep 14, 2023
@rvenancior
Copy link

rvenancior commented Jan 26, 2024

While this is not fixed, we can force the parameter on the child block:

`
// import { useDispatch } from '@wordpress/data';
const { updateBlockAttributes } = useDispatch( 'core/block-editor' );

useEffect( () => {
	const innerBlocks = wp.data.select( 'core/block-editor' ).getBlocks( props.clientId );

	if ( innerBlocks.length > 0 ) {
		innerBlocks.map( ( { name, clientId: innerBlockClientId } ) => {
			if ( 'child-block-name' === name ) {

				updateBlockAttributes( innerBlockClientId, {
					parentAttr1,
					parentAttr2,
				} );
			}
		} );
	}
}, [ parentAttr1, parentAttr2] );

`

@sc0ttkclark
Copy link

Anything I or anyone else can do to help push this forward?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks Needs Dev Ready for, and needs developer efforts [Package] Server Side Render /packages/server-side-render REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.
8 participants