Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#60525 closed defect (bug) (fixed)

Block Bindings: Let the source extend the `uses_context` in the block type

Reported by: gziolo's profile gziolo Owned by: gziolo's profile gziolo
Milestone: 6.5 Priority: normal
Severity: normal Version: 6.5
Component: Editor Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description (last modified by gziolo)

While testing Block Bindings API, we realized that we don't have a robust mechanism for extending block context of supported block types. The approach to manually update every block type's usesContext in block.json file doesn’t scale at all. Hardocing the list of injected useContext entries is also limiting. We decided to formalize it in the registration of the block bindings source. This way, every author of the block bindings source can decide what entries from the block context they want to consume.

Related changes proposed are also in the Gutenberg plugin:

Change History (9)

This ticket was mentioned in PR #6079 on WordPress/wordpress-develop by @santosguillamot.

2 months ago

This pull request adds some logic to add the necessary uses_context defined by block bindings sources. Each source defines the context it needs and it is added to the blocks that are using the block bindings API.

Apart from that, it adapts the post meta and pattern overrides sources for this change.

Finally, it removes the pattern/overrides from the block.json because it is handled this way now.

### Testing instructions

  1. Register a custom field in your site to test it. You can use a code snippet like this:
    		'show_in_rest' => true,
    		'single'       => true,
    		'type'         => 'string',
    		'default'	   => '',

Post meta source test

  1. Go to a page and insert an image block.
  2. Go to the Code editor and connect the image to the custom field by adding the metadata bindings property. It should look something like this:
    <figure class="wp-block-image size-large">[[Image(]]</figure>
  3. Save the page and check that in the frontend, the URL defined in the custom field is used.
  4. Repeat the process for paragraph, heading, and button blocks.

Pattern overrides test

  1. Go to Site Editor -> Patterns -> New pattern.
  2. Create a synced pattern and give it a name.
  3. Add a paragraph with a default value and, in the advanced panel, enable the "Allow instance overrides" setting.
  4. Repeat the process with a heading, an image, and a button.
  5. Go to a page and insert the pattern created.
  6. Modify the value of the blocks and save the page.
  7. Go to the frontend and check that the modified values are used.


Trac ticket:

#2 @gziolo
2 months ago

  • Description modified (diff)

#3 @gziolo
2 months ago

  • Owner set to gziolo
  • Status changed from new to assigned

#4 @gziolo
2 months ago

  • Summary changed from Block Bindings: to Block Bindings: Let the source extend the `uses_context` in the block type

This ticket was mentioned in Slack in #core by gziolo. View the logs.

2 months ago

@santosguillamot commented on PR #6079:

2 months ago

I've created a bunch of tests to check uses_context:

  • Test that the value defined in uses_context is available for the source: link.
  • Test that using a string in uses_context fails: link.
  • Test that the result of registering a source has the correct values: link.
  • Test that multiple sources, with shared context, merge it properly: link.

#7 @gziolo
2 months ago

  • Keywords has-unit-tests commit added

#8 @gziolo
2 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 57641:

Editor: Merge uses_context defined by block bindings sources with block types

Adds logic that fixes the limitation for souces by allowing merging the uses_context defined by block bindings sources into supported block types. Each source defines the context it needs and it is added to the block types that are using the block bindings API.

Fixes #60525.
Props santosguillamot, gziolo, czapla, thekt12.

Note: See TracTickets for help on using tickets.