diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 5afa135d02682b552c2c7df9cb4a223ba085edcf..b7e2806174c3e353bd85783281e839e1d1e96db4 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -763,6 +763,18 @@ public function checkDeprecations(array $context, array $used_variables): void { * The element with the given class(es) in attributes. */ public function addClass(array $element, ...$classes): array { + if (!\is_array($element)) { + return $element; + } + if (\array_is_list($element)) { + foreach ($element as $index => $item) { + if (!\is_array($item)) { + continue; + } + $element[$index] = $this->addClass($item, ...$classes); + } + return $element; + } $attributes = new Attribute($element['#attributes'] ?? []); $attributes->addClass(...$classes); $element['#attributes'] = $attributes->toArray(); @@ -789,6 +801,18 @@ public function addClass(array $element, ...$classes): array { * The element with the given sanitized attribute's value. */ public function setAttribute(array $element, string $name, mixed $value = NULL): array { + if (!\is_array($element)) { + return $element; + } + if (\array_is_list($element)) { + foreach ($element as $index => $item) { + if (!\is_array($item)) { + continue; + } + $element[$index] = $this->setAttribute($item, $name, $value); + } + return $element; + } $element['#attributes'] = AttributeHelper::mergeCollections( $element['#attributes'] ?? [], new Attribute([$name => $value]) @@ -796,7 +820,6 @@ public function setAttribute(array $element, string $name, mixed $value = NULL): // Make sure element gets rendered again. unset($element['#printed']); - return $element; }