Skip to content

Add bs_block_extension_fields() for the fields extensions add to a block - #121

Merged
dnnsjsk merged 2 commits into
inline0:mainfrom
Contributolo:feat/extension-fields-api
Oct 15, 2026
Merged

dnnsjsk merged 2 commits into
inline0:mainfrom
Contributolo:feat/extension-fields-api

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Related to #115.

Problem

bs_block_fields() returns a block's own field definitions. Fields that extensions add to a block (blockstudio.extend) are saved in the same attrs.blockstudio.attributes of the extended block, but their definitions are only reachable through the internal Build::extensions() plus Extensions::get_matches(). A tool that validates saved markup, as described in #115, needs both. Extensions also apply to core and third-party blocks, for which bs_block_fields() returns null.

API

bs_block_extension_fields( string $name ): array

Returns the field definitions of every extension whose name matches the block (exact name, list, or prefix wildcard), using Extensions::get_matches(). It reads $extension->blockstudio['attributes'], so the structure is the same as bs_block_fields(), and the two can be combined:

$fields = array_merge(
  bs_block_fields('core/paragraph') ?? [],
  bs_block_extension_fields('core/paragraph')
);

Design choices:

  • Separate function instead of an argument to bs_block_fields(). bs_block_fields() uses null to mean "unknown or not a Blockstudio block", which would not fit core blocks with extension fields. Matching is by name only, like on render, so a block no extension matches returns [].
  • One flat list instead of a list per extension. All extensions write into the block's single attrs.blockstudio.attributes object, next to the block's own values, so ids share one namespace. A flat list can be walked like bs_block_fields(). Panel group and priority only affect the editor UI.
  • Registration order. This is the order Block::render() and Extensions::render_blocks() resolve extension definitions in: if two extensions define the same id, both definitions are returned, they edit the same value, and on render the definition of the extension registered later applies. The editor sorts panels by priority, which is presentation only.

The PR also makes Extensions::get_matches() return an extension once when several of its names match (for example ["core/*", "core/paragraph"]), as the editor already does with some(). Without that, the new helper would list such fields twice. Rendering is unaffected because both callers key the result by attribute id.

Docs: a paragraph in "Inspecting Blocks" (blocks/rendering.md) and a short "Inspecting Extension Fields" section in extensions.md. The LLM context is rebuilt with npm run build:llm, nothing else changed in it. No changelog entry, since the 7.7.2 notes are already prepared. A possible line: New: public bs_block_extension_fields() helper exposes the field definitions extensions add to a block. @since 7.7.3 assumes the next release; please adjust if needed.

Tests

  • ExtensionsTest: an extension with several matching names is returned once.
  • BlockMetadataTest (registers its own extensions and restores the registry afterwards):
    • prefix wildcard unit-meta/*, exact name unit-meta/card, a name list, and core/paragraph; blocks matched by none (core/heading, unit-metadata/card, unknown) return []
    • a Blockstudio block keeps its own fields in bs_block_fields() and gets extension fields only from the new helper
    • two extensions with the same id: both are returned, and Extensions::render_blocks() applies the later one
    • the extension files of the test theme (core/*, core/heading, the blockstudio/type-text list) are reported for the expected blocks

composer cs, PHPStan and npm run check pass. The unit tests ran in WordPress Playground with the test theme active (no Docker here): tests/unit/block, extensions, render, register, attribute-builder, block-discovery, admin-page and canvas pass. Without the get_matches() change, the new duplicate test and the matching test in BlockMetadataTest fail.

An extension whose name list contains several patterns that match a block,
such as ["core/*", "core/paragraph"], was returned once per matching
pattern. The editor matches with some() and shows the extension once.
Stop after the first matching name so the server side agrees.
bs_block_fields() returns a block's own field definitions, but fields that
extensions add are stored in the same attrs.blockstudio.attributes and were
only reachable through Build::extensions(). The new helper returns the
field definitions of every extension matching a block name, using
Extensions::get_matches(), so it also covers core and third-party blocks.

@dnnsjsk dnnsjsk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the helper against the registry and both extension render paths. The separate array-returning API correctly covers core/third-party targets without changing bs_block_fields() null semantics. The matching fix preserves registration order while removing duplicate matches of the same extension, and the unit tests cover the lookup and later-extension precedence. Including this in 7.7.2; the since annotation and changelog will be adjusted in the release follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants