Closed larowlan closed 3 years ago
I really like the idea of immutable values when using pure JavaScript functions.
However, we can see that Drupal core's Attribute object is a mutable object. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/9.1.x
And we can test this in a Twig file with the following:
{% set testAttr = create_attribute({ data: 'no-classes-by-default'}) %}
<div {{ testAttr }}></div>
<div {{ testAttr.addClass('test1') }}></div>
<div {{ testAttr.addClass('test2') }}></div>
which, when run through Drupal core outputs:
<div data="no-classes-by-default"></div>
<div data="no-classes-by-default" class="test1"></div>
<div data="no-classes-by-default" class="test1 test2"></div>
I think we ended up getting around this by being sure to call create_attribute
when needed
I don't like it either that they are mutable. But unfortunately, some Drupal templates may rely on the fact that they are mutable and, since this library is basically a compatiblity library, enabling writing and testing Twig templates outside of Drupal, it has to remain this way.
We've integrated this into kss-node so that we can use drupal attributes in our styleguide twig files, but we're seeing attributes bleed into lower templates.
Making the method calls immutable and returning new instances should resolve this.