Skip to content

Add an opt-in "join" to "set" for list values - #124

Closed
Contributolo wants to merge 2 commits into
inline0:mainfrom
Contributolo:feat/set-list-values
Closed

Contributolo wants to merge 2 commits into
inline0:mainfrom
Contributolo:feat/set-list-values

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Extension set rules evaluate their template once per entry of a list value (select with multiple, checkbox, token). For class and style every entry adds a class or declaration, but any other attribute is written with WP_HTML_Tag_Processor::set_attribute() once per entry, so only the last entry survives. That is right for single-valued attributes like href or id, but a token list such as data-langs with two chosen languages renders data-langs="de".

This adds an opt-in join to a set entry:

"set": [{ "attribute": "data-langs", "value": "{attributes.langs}", "join": true }]

With "join": true, the evaluated values of a list are written once, separated by single spaces like an HTML token list, so [data-langs~="en"] matches every chosen entry. Empty and repeated results are skipped, and nothing is written when no result remains. Without join (or with "join": false) every attribute behaves as on main: the last entry wins. join has no effect on class and style or on single values.

In the editor, each list entry is now also the field's value while its template is evaluated, which is what render_blocks() already does, and stored token entries ({ value, label }) are reduced to the return format like transform_option_attribute(). On main the editor wrote the unresolved placeholder for returnFormat: "both" lists and [object Object],[object Object] for token fields while the frontend wrote the last entry; editor and frontend now match.

Changes

  • Extensions::render_blocks(): opt-in join for attributes other than class and style; helper Extensions::join_list_values()
  • src/blocks/filters/extensions.tsx: the same opt-in through joinListValues() (src/blocks/utils/join-list-values.ts), per-entry evaluation, token reduction
  • schemas/extend.json: set items with attribute, value, join (checksum updated)
  • Docs: extensions.md under "Arrays" with an example, LLM context regenerated

Tests

  • tests/unit/extensions/ExtensionSetTest.php (18 tests): href and id with two values (last wins), data attribute without join and with join: false, returnFormat: "both" and tokens with and without join, repeated and empty results, empty list, class and style with and without join, single value. On main 5 fail (the join cases), on the first commit of this PR 6 fail (the cases without join)
  • tests/unit/extensions/ExtensionsTest.php: join_list_values()
  • tests/client/join-list-values.test.ts: joinListValues()
  • tests/e2e/types/other/extension-set.ts with the fixture extensions/set-list.json on core/verse: frontend, editor canvas and the wrapperProps of the extension filter for the same stored values, before and after checking another option and saving

Checked locally in WordPress Playground CLI: the new e2e test, types/text.ts, the extensions, field, render, block and attribute-builder unit suites, npm run check, PHPCS and PHPStan. The bundle in includes/admin/assets is not rebuilt in this PR.

Extension "set" rules evaluate their template once per entry of a list
value (multiple select, checkbox, token). For class and style every entry
adds a class or a declaration, but any other attribute was written with
WP_HTML_Tag_Processor::set_attribute() once per entry, so only the last
entry survived: choosing two languages rendered data-langs="de". The
editor filter overwrote its wrapper props the same way.

The evaluated values of a list are now collected and written once,
separated by single spaces like an HTML token list, so selectors such as
[data-langs~="en"] match every chosen entry. Empty and repeated results
are skipped, and a list without any result writes nothing, as an empty
list already did. Single values and the class and style handling are
unchanged.

In the editor, each list entry is now also the field's value while its
template is evaluated, as render_blocks() already does. Without that,
token lists showed "a,b" and lists with returnFormat "both" kept the
unresolved placeholder in the editor.

@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.

Joining values solves the data-langs example, but the implementation applies the new semantics to every attribute except class/style. Single-valued attributes are affected too: a valid existing set binding targeting href with values /first/ and /second/ now emits href="/first/ /second/" instead of the previous /second/. IDs and other single-valued attributes have the same compatibility problem. I verified the new join_list_values() returns the combined URL string, and both render_blocks() and the editor path assign that result unconditionally.

Please limit aggregation to attributes with defined token-list semantics or make the joining policy explicit/opt-in, preserving existing behavior for single-valued URL/ID bindings. Add server and editor regressions for href/id as well as the intended data/token-list attributes. This is a behavior change beyond the data attribute example, so I am holding the current version out of 7.7.2.

The previous commit joined the evaluated values of a list for every
attribute other than class and style. That changed existing bindings to
single-valued attributes: two links bound to href rendered
href="/first/%20/second/" instead of "/second/", and an id received two
tokens.

Joining is now opt-in per set entry with "join": true. Without it, every
entry of a list overwrites the attribute and the last one wins, in
render_blocks() and in the editor, as before. With it, the results of
all entries are written once, separated by single spaces; empty and
repeated results are skipped, and nothing is written when no result
remains. "join" has no effect on class and style, which add one class or
declaration per entry either way, or on a field that returns a single
value. The option is documented and declared in the extension schema.

The editor still evaluates each list entry as the field's value, as
render_blocks() does. It now also reduces stored token entries
({ value, label }) to the return format like the server, so token lists
no longer render "[object Object]" in the editor.

Server regressions cover href, id, data attributes, tokens and
returnFormat "both" with and without join, empty and repeated results,
class and style. An e2e test compares the editor wrapper (its DOM and
the props the extension filter passes) with the frontend for the same
stored values, before and after a checkbox is changed in the inspector.
@Contributolo

Copy link
Copy Markdown
Collaborator Author

Thanks for the review, agreed. Reworked in 47ea601:

  • Joining is now opt-in per set entry with "join": true. Without it, every attribute other than class and style behaves as on main: each list entry overwrites the attribute and the last one wins, in render_blocks() and in the editor. The href binding with /first/ and /second/ renders href="/second/" again, ids likewise.
  • With "join": true, the results of all entries are written once, separated by single spaces. Empty and repeated results are skipped, and nothing is written when no result remains. join has no effect on class and style (one class or declaration per entry, as before) or on single values. It is documented in extensions.md with an example and declared in the extension schema (set items with attribute, value, join), checksum and LLM context updated.
  • Editor: each list entry is still evaluated as the field's value, as render_blocks() does. On main the editor wrote the unresolved placeholder for returnFormat: "both" lists and [object Object],[object Object] for token fields, even without join, while the frontend wrote the last entry. It now matches the frontend. Tokens are stored as { value, label }, so the editor reduces them to the return format like transform_option_attribute(). Select, radio and checkbox bindings with value or label format are unchanged.
  • Server regressions in ExtensionSetTest: href and id with two values (last wins), a data attribute without join and with join: false, returnFormat: "both" and tokens with and without join, repeated and empty results, an empty list, class and style with and without join, a single value. On main 5 of 18 fail (only the join cases), on the previous commit 6 fail (href, id, data, both and token without join).
  • Editor regression in tests/e2e/types/other/extension-set.ts (fixture extensions/set-list.json on core/verse): the same stored values are compared on the frontend, in the editor canvas and in the wrapperProps the extension filter passes (the block editor replaces the wrapper id with its own, so id is only visible there), then again after checking another option in the inspector and saving. It fails on main (join) and on the previous commit (href, id), and passes now. The bundle in includes/admin/assets is not rebuilt in this PR, so the editor part needs a build.
  • Ran locally against WordPress in Playground CLI: the new e2e test, types/text.ts, the extensions, field, render, block and attribute-builder unit suites (same results as main apart from the new cases), npm run check, PHPCS and PHPStan.

Unchanged and the same on main: the server sorts checkbox selections into option order while the editor keeps the stored order, so with options chosen out of order the editor can show a different last entry or token order than the frontend. I can send that as a separate PR if you want it.

I also updated the title and description of this PR to the opt-in behavior.

@Contributolo Contributolo changed the title Join list values when "set" writes an attribute other than class Add an opt-in "join" to "set" for list values Oct 16, 2026
@dnnsjsk

dnnsjsk commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.4.

The opt-in join behavior for extension set rules has shipped, with matching editor/frontend behavior and regression coverage.

Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution.

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