Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor of Attribute doesn't match Drupal core #1

Open
JohnAlbin opened this issue Apr 9, 2021 · 4 comments · May be fixed by #54
Open

Constructor of Attribute doesn't match Drupal core #1

JohnAlbin opened this issue Apr 9, 2021 · 4 comments · May be fixed by #54
Labels
bug Something isn't working

Comments

@JohnAlbin
Copy link
Owner

JohnAlbin commented Apr 9, 2021

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

@JohnAlbin JohnAlbin added the bug Something isn't working label Apr 9, 2021
@JohnAlbin
Copy link
Owner Author

JohnAlbin commented Apr 9, 2021

It looks like Twig.js converts the Twig associative array syntax into a JS object internally. And trying to pass an object to Attribute's constructor errors with object is not iterable (cannot read property Symbol(Symbol.iterator))

@MichaelAllenWarner
Copy link

Hi @JohnAlbin,

I think I have a fix for this, and also a way to get property-access working in both Twing and Twig.js (the {{ attributes.style }} syntax). It all builds on top of #53, though. Would it make sense for me to scrap #53 and replace it with one big PR that addresses all create_attribute() problems? Or would you rather I proceed incrementally?

@MichaelAllenWarner
Copy link

For context, here is my feature-branch that has all create_attribute() fixes: https://github.com/MichaelAllenWarner/drupal-twig-extensions/tree/fix-create_attributes

Diff: main...MichaelAllenWarner:drupal-twig-extensions:fix-create_attributes

(So the "big PR" would be for all this.)

@MichaelAllenWarner MichaelAllenWarner linked a pull request Apr 29, 2023 that will close this issue
@MichaelAllenWarner
Copy link

I went ahead and made the "big" PR (#54) and closed #53.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants