Block Bindings: Enable navigation links to use any binding source + readOnly attribute#76634
Block Bindings: Enable navigation links to use any binding source + readOnly attribute#76634
Conversation
Extends the navigation link block to support any registered block binding source while maintaining full backward compatibility with existing WP 6.9 navigation links. Key changes: - Implements args-first resolution in core/post-data and core/term-data sources - Three-tier fallback: binding args → block attributes → block context - Updates useEntityBinding to detect any registered source via getBlockBindingsSource() - Adds 'id' to supported bindable attributes for nav-link and submenu - Adds comprehensive PHP integration tests (17 tests) covering both legacy and new formats - Updates JavaScript unit tests with registry-based source detection This enables navigation links to bind to custom sources like policy pages while ensuring zero breaking changes for existing blocks.
Introduces a readOnly attribute that allows navigation links to have locked URLs while keeping labels editable. When readOnly is true: - Label remains editable via RichText - URL and ID cannot be changed - Link toolbar button is disabled - LinkPicker shows link info without dropdown functionality - Chevron icon is hidden from LinkPreview This enables use cases where links should be bound to specific sources (like policy pages from WordPress settings) without allowing URL changes.
The isHomepage() check was incorrectly identifying draft pages as the homepage because draft URLs like "/?p=123" have the same pathname (/) as the homepage but include query parameters. Added a check to exclude URLs with query parameters from being identified as the homepage, since the homepage should be a clean URL without query strings.
Removed args-first resolution tier. Entity ID/type now determined by: - Navigation blocks: Read from block attributes (id, type) - Other blocks: Read from block context (postId/termId, postType/taxonomy) This is simpler and achieves the same goal - allowing custom binding sources to work with navigation links while maintaining backward compatibility with WP 6.9+ navigation links. Updated: - JS: post-data.js, term-data.js, use-entity-binding.js - PHP: post-data-block-bindings.php, term-data-block-bindings.php - Tests: use-entity-binding.js (updated assertions)
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
Size Change: +238 B (0%) Total Size: 8.76 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in 4d1baac. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23257731236
|
…serEditValue Replaces the custom readOnly attribute with the Gutenberg-native canUserEditValue mechanism from block binding sources. This allows binding sources to control whether navigation links are editable, enabling custom sources to create truly read-only links while keeping core bindings editable. Changes: - Remove readOnly attribute from navigation-link block.json - Derive isLinkEditable from binding sources' canUserEditValue - Update core/post-data and core/term-data to return true for navigation blocks - Prevent overwriting custom binding sources with core bindings - Update documentation
c028166 to
9619a44
Compare
|
Working on moving the PHP binding source changes to the |
|
Update: Currently investigating how to properly structure the PHP changes for the |
9619a44 to
4d1baac
Compare
This PR is NOT ready for merge as-is. It's intentionally large to demonstrate the complete UI experience and developer interface for custom navigation link bindings. Before shipping, this needs to be broken into smaller, focused PRs.
Purpose: To allow reviewers and stakeholders to:
Next steps: After review and consensus, we'll split this into logical incremental PRs that can be safely merged.
What
Enables Navigation Link blocks to use any registered block binding source, not just
core/post-dataandcore/term-data. Binding sources control link editability through theircanUserEditValuefunction.Why
Navigation Links were hardcoded to only work with two core binding sources. This prevented plugins from creating custom binding sources for navigation links—use cases like WooCommerce policy pages, custom settings pages, or plugin-managed links.
How
The block now accepts any registered binding source instead of hardcoding allowed sources. Link editability is controlled by the binding source's
canUserEditValuefunction: when it returnsfalse, the URL becomes read-only while the label remains editable. Core binding sources (core/post-data,core/term-data) remain editable for navigation blocks, while custom sources can enforce read-only behavior as needed.Also fixes a bug where draft pages were incorrectly badged as "Homepage" in the editor.
Testing Instructions
Test 1: Regular Navigation Links
Test 2: Custom Read-Only Binding Source
canUserEditValue: () => false:<!-- wp:navigation-link {"label":"Privacy Policy","type":"page","kind":"post-type","metadata":{"bindings":{"url":{"source":"my-plugin/custom-source","args":{}},"id":{"source":"my-plugin/custom-source","args":{}}}}} /-->Test 3: Homepage Badge Fix
Implementation Notes
canUserEditValueis only registered ifsetValuesis also defined (even for read-only sources, add a no-opsetValues)truefor navigation blocks (they use attributes instead of context for entity data)falseto create read-only links