Make WordPress Core

Opened 11 months ago

Closed 11 months ago

Last modified 5 months ago

#58929 closed defect (bug) (invalid)

Inner Blocks are not rendered within custom blocks

Reported by: sebastiande's profile sebastiande Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.2.2
Component: Editor Keywords:
Focuses: Cc:

Description

Hi,

When using custom blocks within the editor there is always an AJAX call against the "block renderer" (/wp-json/wp/v2/block-renderer/*). This ignores inner blocks completely even if it is delivered through the request. Basically it "fakes" the render_blocks functionality and therefore the inner blocks are not rendered and no preview is available.

<?php
// Create an array representation simulating the output of parse_blocks.
$block = array(
  'blockName'    => $request['name'],
  'attrs'        => $attributes,
  'innerHTML'    => '',
  'innerContent' => array(),
);

// Render using render_block to ensure all relevant filters are used.
$data = array(
  'rendered' => render_block( $block ),
);

Wouldn't it be better to actually parse the blocks and use the inner_blocks from the request to render a better preview?

Actually I fixed it in my theme (very ugly):

<?php
function modify_render_block_data_defaults($parsed_block, $source_block, $parent_block) {
    // inner_blocks GET param should only be set within ajax preview requests
    if (!isset($_GET['inner_blocks'])) {
        return $parsed_block;
    }

    $innerBlocks = $_GET['inner_blocks'];
    // if we do not have inner blocks we do not have to apply this hack
    if (empty($innerBlocks)) {
        return $parsed_block;
    }

    $blockName = $parsed_block['blockName'];
    $attributes = $parsed_block['attrs'];

    $completeBlock = "<!-- wp:" .  $blockName;
    if (is_array($attributes) && !empty($attributes)) {
        $completeBlock .= " " . json_encode($attributes);
    }
    $completeBlock .= " -->";
    $completeBlock .= urldecode_deep($innerBlocks) . "<!-- /wp:" . $blockName . " -->";

    return parse_blocks($completeBlock)[0];
}
add_filter( "render_block_data", "modify_render_block_data_defaults", 10, 3 );

with this, the block is fully rendered as it should. It would be awesome if you could fix the function above using parse_blocks.

Change History (2)

#1 @sebastiande
11 months ago

  • Resolution set to invalid
  • Status changed from new to closed

#2 @swissspidy
5 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.