- Issue created by @mherchel
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
So this would require changing
\Drupal\Core\Template\TwigExtension::addClass()
and friends.IMHO Twig Filters do not make sense to apply to an array of render arrays. The problem grows more complex too: what about an array of arrays of render arrays (i.e. with an extra level of indirection)? What about an array that contains both render arrays directly and indirectly?
The better solutions IMHO, because they would be more reliable:
- — captured as in 📌 [SPIKE] Comprehensive plan for integrating with SDC Active
- restricting what the accepted contents are of a slot — captured as in
📌
[SPIKE] Comprehensive plan for integrating with SDC
Active
. ← Applied to this issue, that would mean that a slot only accepts a render element (i.e.
['#type' => …, … => …]
) render array, not an arbitrary render array. That'd then allow using theadd_class
Twig filter without changing it.
- 🇫🇷France pdureau Paris
Hi,
we have overridden Core's
|add_class()
andset_attribute()
filters in UI Patterns (1 & 2) since May 2023, and... it works.https://git.drupalcode.org/project/ui_patterns/-/blob/2.0.x/src/Template...
what about an array of arrays of render arrays?
As long as the array is a list, as expected.
It is a simple and useful solution, used by many projects for many months. No need to introduce complicated concepts like "repeatable slots" or "slots restrictions"
- 🇺🇸United States nicxvan
Yeah we need something like this.
Especially with SDCs it's becoming common for me to use set_attribute to the next level below.
So for example this works to set the slot attribute if link is a single value.
button: field_link|set_attribute('slot', 'button__link'),
However if link is multivalue, none of the links get the attribute.
In the parent issue it was suggested to do this:
{% for index,item in my_list_of_renderable_arrays %} {{ item|add_class('item', 'item--' ~ index) }} {% endfor %}
but are you excepted to rebuild that as an array? I'd expect it to set the attribute for everything in the list.
- 🇺🇸United States nicxvan
I feel like when it's a multi value field it should always apply to all items.
- 🇫🇷France pdureau Paris
Hi @nicxvan, you have open a MR, can you assign the issue to you so people will know you are working on it?
- 🇫🇷France nod_ Lille
MR has the code for setattribute, need to do that for add_class too I guess?
- 🇺🇸United States nicxvan
I've pushed up the class override, I'm not actively working on this at the moment so no need to assign myself.
This needs tests.