Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#60696 new enhancement

Block hooks: Allow insertion of a hooked block with attributes

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

Description

Previously, the hooked_block_types and hooked_block_{$hooked_block_type} filters were introduced to allow templates to be extended with blocks by third parties.

While those two filters work well with unique blocks, they prove challenging when working with generic blocks that can be customized via attributes. Attempting to hook a block into an area that has already inserted the same block type, but with different attributes won't work with the existing filters.

[This PR](https://github.com/woocommerce/woocommerce/pull/44860) demonstrates how attempting to set attributes via the hooked_block_{$hooked_block_type} filter will overwrite all of the same block types that have been hooked in.

It may be possible that block hooks aren't the appropriate venue for this API, but a way to insert blocks with attributes into templates is quite useful, even in the context of the core editor.

Requirements:

  • [ ] Allow insertion of blocks with block attributes in a single filter
  • [ ] Allow checking anchor block properties before inserting hooked blocks
  • [ ] (optional, but preferred) Allow nesting hooked blocks onto previous hooked blocks

Change History (1)

This ticket was mentioned in PR #6228 on WordPress/wordpress-develop by joshuatf.


9 months ago
#1

  • Keywords has-patch added

This PR is a POC that introduces a new hook hooked_blocks that allows insertion of an entire hooked block, including its attributes. This is beneficial for inserting generic blocks into templates that are only differentiated by their attributes. It _should be_ fully backwards compatible existing block hook filters. cc @ockham

To avoid the issue with ignoredHookedBlocks blocking generic blocks from being added of similar types, it also adds a hooked_block_signature to help discern hooked blocks.

  • Block signature without attributes: woocommerce/text-field
  • Block signature with attributes: woocommerce/text-field-{"label":"Name"}

Note that it's possible this should be a separate API from block hooks, but it seems the core editor could also benefit from the ability to use generic blocks.

Trac ticket: https://core.trac.wordpress.org/ticket/60696#ticket

Note: See TracTickets for help on using tickets.