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

blocksJSON doesn't return reusable block data #142

Open
andrewmumblebee opened this issue Mar 22, 2022 · 0 comments
Open

blocksJSON doesn't return reusable block data #142

andrewmumblebee opened this issue Mar 22, 2022 · 0 comments

Comments

@andrewmumblebee
Copy link

andrewmumblebee commented Mar 22, 2022

When using the blocksJSON property in a query against a post/page then the data is returned correctly for most blocks, but if there is a reusable block in use then the innerBlocks of it isn't populated.

We don't drill down into our blocks through graphQL as we don't generally know the max depth of blocks an editor might use, and it saves us creating a complex nested query.

So the blocksJSON property has been a savior for us.

To fix this i've changed this part

$this->innerBlocks = self::create_blocks($data['innerBlocks'], $post_id, $registry, $this);

To this

public function __construct( $data, $post_id, $registry, $order, $parent ) {

	$innerBlocks = $data['innerBlocks'];

	// handle mapping reusable blocks to innerblocks.
	if ( $data['blockName'] === 'core/block' && ! empty( $data['attrs']['ref'] ) ) {
		$ref            = $data['attrs']['ref'];
                $reusablePost = get_post( $ref );

		if ( ! empty( $reusablePost ) ) {
			$innerBlocks = parse_blocks( $reusablePost->post_content );
		}
	}

	$this->innerBlocks = self::create_blocks( $innerBlocks, $post_id, $registry, $this );

   ... rest of code

Then the innerBlocks of a reusable block should be the same as the blocks that were stored in the post created for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant