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

Created on 17 April 2023, over 1 year 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 1 hour ago

Created by

🇺🇸United States mherchel Gainesville, FL, US

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

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.
Production build 0.71.5 2024