Make WordPress Core

Opened 5 months ago

#60587 new enhancement

Block hooks: Allow filtering of block data on traversal

Reported by: joshuaflow's profile joshuaflow Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Editor Keywords:
Focuses: Cc:

Description

When parsing blocks, it would be useful to be able to update block attributes, add metadata, or even potentially update a block name in a template for use in the client.

In the WooCommerce product editor, block templates are used "statically" in that the product form template is provided to the merchant without the ability to add/remove/reorder blocks. If static templates are a valid use case for blocks, I believe many static template blocks will need to be editable to allow third parties some level of configuration. In this case, we need to allow adding of metadata to blocks to control their visibility, but this can also be useful for updating the titles of blocks or adding block bindings to the templates.

<?php
$block_to_modify = array(
  'blockName' => 'woocommerce/product-tab',
  'attrs'     => array(
    'id'       => 'pricing',
    'title'    => 'Pricing', // Extensions may want to update this property.
    'metadata' => array(), // Need the ability to add product type association, block bindings, and block hiding conditions here.
  ),
);

Templates and template parts that are parsed and retrieved via the REST API pass through the [traverse_and_serialize_blocks](https://github.com/WordPress/wordpress-develop/blob/bd78247617da4e23c02876423f400030eb0c75a3/src/wp-includes/blocks.php#L1356) function. Adding a hook to the top of this function would allow flexibility in not only static templates, but also traditional persisted templates where an extension wants to replace block types with newly added blocks.

<?php
$block = apply_filters( 'pre_traverse_and_serialize_block', $block );

Note that this is already possible by hooking into get_block_templates and re-traversing the tree but is less convenient and means poorer performance since all templates will have to be traversed at least twice.

Change History (0)

Note: See TracTickets for help on using tickets.