Description
The current implementation of Attribute uses a JavaScript Map internally, which is an excellent choice.
But the constructor of Attribute just re-uses the Map constructor, so usage is:
const attr = new Attribute([['id', 'val'], ['class', ['one', 'two']]]);
or in a Twig file:
{% set attr = create_attribute([['id', 'val'], ['class', ['one', 'two']]]) %}
See the Map constructor docs:
Parameters
iterable (Optional)
An Array or other iterable object whose elements are key-value pairs. (For example, arrays with two elements, such as[[ 1, 'one' ],[ 2, 'two' ]]
.) Each key-value pair is added to the new Map.
But that doesn't match the syntax we'd use inside Drupal twig files because Twig associative arrays use an object-like syntax:
{% set attr = create_attribute({ id: "val", class: ["one", "two"]}) %}
I've implemented a work-around inside the create_attribute()
Twig function.
The fault is either in:
- the Attribute constructor
- the JS Twig implementation that treats the object-like Twig syntax like an object.
We either need to provide a patch upstream or fork drupal-attribute
into this project.
Docs for Drupal's Attribute object are here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/9.1.x