WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.37k stars 4.15k forks source link

Pass parent block context during server side rendering #40714

Open Hug0-Drelon opened 2 years ago

Hug0-Drelon commented 2 years ago

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?

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 commented 2 years ago

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.

EmranAhmed commented 1 year ago

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

tanvirulhaque commented 1 year ago

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 commented 1 year ago

Yes, we definitely want to see this issue resolved!

It looks like https://github.com/WordPress/gutenberg/issues/34882 covers the same issue.

sovetski commented 1 year ago

Same here

rvenancior commented 8 months ago

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 commented 4 months ago

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

wpexplorer commented 1 month ago

bump ;)