[TwigComponent] Add support for AttributeValueInterface from twig/html-extra:^3.24.0 in ComponentAttributes#3408
Merged
Kocal merged 1 commit intosymfony:2.xfrom Mar 23, 2026
Conversation
…html-extra:^3.24.0` in `ComponentAttributes` Close twigphp/Twig#4790, replace twigphp/Twig#4791. This PR update `ComponentAttributes` to support `AttributeValueInterface` from Twig 3.24 with `html_attr_type` and HTML attributes merging strategy. This helps resolve situations where merging HTML attributes needs to be more sophisticated than a simple `array_merge`. For example in UX Toolkit, we have an issue where it's not possible to use a single `<twig:Button>` with `Dialog` and `Tooltip` triggers, both triggers define a `trigger_attrs` with some attributes that may conflict. Here a simplified version: ``` {%- set dialog_trigger_attrs = { 'data-action': 'click->dialog#open', } -%} {%- set tooltip_trigger_attrs = { 'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide', } -%} <twig:Button {{ ...dialog_trigger_attrs }} {{ ...tooltip_trigger_attrs }} /> ``` Here, only `data-action="mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide"` will be rendered, the value from `dialog_trigger_attrs` is purely ignored. By supporting the HTML attributes merging strategy introduced in Twig HTML Extra 3.24, we can use the new Twig filter `html_attr_type`: ```twig {%- set dialog_trigger_attrs = { 'data-action': 'click->dialog#open'|html_attr_type('sst'), } -%} {%- set tooltip_trigger_attrs = { 'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide'|html_attr_type('sst'), } -%} ``` Combined to `html_attr_merge` (that return an array where some values are an instance of `Twig\Extra\Html\HtmlAttr\AttributeValueInterface`), the following example will correctly render `data-action="click->dialog#open mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide"`: ```twig <twig:Button {{ ...{}|html_attr_merge(dialog_trigger_attrs, tooltip_trigger_attrs) }} /> ```
65eb44d to
bd2410f
Compare
This was referenced Mar 23, 2026
Kocal
added a commit
to Kocal/symfony-ux
that referenced
this pull request
Mar 23, 2026
…rrectly merge trigger's attributes Related to symfony#3408
Kocal
added a commit
to Kocal/symfony-ux
that referenced
this pull request
Mar 23, 2026
…rrectly merge trigger's attributes Related to symfony#3408
Kocal
added a commit
that referenced
this pull request
Mar 23, 2026
…a:^3.24` to correctly merge trigger's attributes (Kocal) This PR was merged into the 2.x branch. Discussion ---------- [Toolkit] Embrace `html_attr_type` from `twig/html-extra:^3.24` to correctly merge trigger's attributes | Q | A | -------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | no <!-- required for new features, or documentation updates --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Related to #3408 Commits ------- 725c115 [Toolkit] Embrace `html_attr_type` from `twig/html-extra:^3.24` to correctly merge trigger's attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close twigphp/Twig#4790, replace twigphp/Twig#4791.
This PR update
ComponentAttributesto supportAttributeValueInterfacefrom Twig 3.24 withhtml_attr_typeand HTML attributes merging strategy.This helps resolve situations where merging HTML attributes needs to be more sophisticated than a simple
array_merge.For example in UX Toolkit, we have an issue where it's not possible to use a single
<twig:Button>withDialogandTooltiptriggers, both triggers define atrigger_attrswith some attributes that may conflict. Here a simplified version:{%- set dialog_trigger_attrs = { 'data-action': 'click->dialog#open', } -%} {%- set tooltip_trigger_attrs = { 'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide', } -%} <twig:Button {{ ...dialog_trigger_attrs }} {{ ...tooltip_trigger_attrs }} />Here, only
data-action="mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide"will be rendered, the value fromdialog_trigger_attrsis purely ignored.By supporting the HTML attributes merging strategy introduced in Twig HTML Extra 3.24, we can use the new Twig filter
html_attr_type:{%- set dialog_trigger_attrs = { 'data-action': 'click->dialog#open'|html_attr_type('sst'), } -%} {%- set tooltip_trigger_attrs = { 'data-action': 'mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide'|html_attr_type('sst'), } -%}Combined to
html_attr_mergefilter (that return an array where some values are an instance ofTwig\Extra\Html\HtmlAttr\AttributeValueInterface), the following example will correctly renderdata-action="click->dialog#open mouseenter->tooltip#show mouseleave->tooltip#hide focus->tooltip#show blur->tooltip#hide":