Skip to content
Snippets Groups Projects

Set Attribute for lists

Open nicxvan requested to merge issue/drupal-3354683:3354683-decide-how-twig into 11.x
1 file
+ 24
1
Compare changes
  • Side-by-side
  • Inline
@@ -763,6 +763,18 @@ public function checkDeprecations(array $context, array $used_variables): void {
@@ -763,6 +763,18 @@ public function checkDeprecations(array $context, array $used_variables): void {
* The element with the given class(es) in attributes.
* The element with the given class(es) in attributes.
*/
*/
public function addClass(array $element, ...$classes): array {
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 = new Attribute($element['#attributes'] ?? []);
$attributes->addClass(...$classes);
$attributes->addClass(...$classes);
$element['#attributes'] = $attributes->toArray();
$element['#attributes'] = $attributes->toArray();
@@ -789,6 +801,18 @@ public function addClass(array $element, ...$classes): array {
@@ -789,6 +801,18 @@ public function addClass(array $element, ...$classes): array {
* The element with the given sanitized attribute's value.
* The element with the given sanitized attribute's value.
*/
*/
public function setAttribute(array $element, string $name, mixed $value = NULL): array {
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'] = AttributeHelper::mergeCollections(
$element['#attributes'] ?? [],
$element['#attributes'] ?? [],
new Attribute([$name => $value])
new Attribute([$name => $value])
@@ -796,7 +820,6 @@ public function setAttribute(array $element, string $name, mixed $value = NULL):
@@ -796,7 +820,6 @@ public function setAttribute(array $element, string $name, mixed $value = NULL):
// Make sure element gets rendered again.
// Make sure element gets rendered again.
unset($element['#printed']);
unset($element['#printed']);
return $element;
return $element;
}
}
Loading