Decide how Twig filters should process against an array of render arrays

Created on 17 April 2023, almost 2 years ago

Problem/Motivation

This issue arrises from Create twig filters: |add_class and |set_attribute Fixed , where there is discussion on how the |add_class filter should work when it runs into an array of render arrays.

From @pdureau

I am afraid it will not work because the themers don't know if the renderable they print is a single render array or a list of them.

For example, without talking about those new filters, because a list of renderable is also a renderable, it is already safer for themers to print directly the variable:

From @lauriii

Thanks for the feedback @pdureau! That seems like a valid use case, but OTOH we don't generally apply filters across child elements like what you are proposing. I'm wondering if this could be handled on the render array level by introducing a new type that allows inheriting properties to children?

Something like this:

[
  '#type' => 'inherit',
  '#attributes' => ...
  [
    '#type' => 'custom_button',
    '#label' => 'Hello',
  ],
  [
    '#type' => 'custom_button',
    '#label' => 'Hello',
  ]
]

I guess there could be other ways to create similar functionality. I'm not too keen to what I'm proposed here so if someone has better ideas, I'd be open to that.

📌 Task
Status

Active

Version

10.1

Component
Theme 

Last updated about 24 hours ago

Created by

🇺🇸United States mherchel Gainesville, FL, US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • 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:

    1. — captured as in 📌 [SPIKE] Comprehensive plan for integrating with SDC Active
    2. 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 the add_class Twig filter without changing it.
  • 🇫🇷France pdureau Paris

    Hi,

    we have overridden Core's |add_class() and set_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.

  • Merge request !11524Set Attribute for lists → (Open) created by nicxvan
  • Pipeline finished with Success
    15 days ago
    Total: 891s
    #451425
  • 🇫🇷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.

  • Pipeline finished with Success
    7 days ago
    Total: 612s
    #458036
Production build 0.71.5 2024