You are here

public static function TwigExtension::setFilter in Components! 3.x

Same name and namespace in other branches
  1. 8.2 src/Template/TwigExtension.php \Drupal\components\Template\TwigExtension::setFilter()

Sets a deeply-nested property on an array.

If the deeply-nested property exists, the existing data will be replaced with the new value.


{{ form|set( 'element.#attributes.placeholder', 'Label' ) }}

Parameters

array|iterable|\Traversable $element: The parent renderable array to set into.

string $at: The dotted-path to the deeply nested element to set.

mixed $value: The value to set.

Return value

array The merged renderable array.

Throws

\Twig\Error\RuntimeError When $element is not an array or "Traversable".

2 calls to TwigExtension::setFilter()
TwigExtensionFiltersTest::testSetFilter in tests/src/Unit/TwigExtensionFiltersTest.php
Tests the set filter.
TwigExtensionFiltersTest::testSetFilterException in tests/src/Unit/TwigExtensionFiltersTest.php
Tests exceptions during set filter.

File

src/Template/TwigExtension.php, line 157

Class

TwigExtension
A class providing components' Twig extensions.

Namespace

Drupal\components\Template

Code

public static function setFilter($element, string $at, $value) {
  if (!twig_test_iterable($element)) {
    throw new RuntimeError(sprintf('The set filter only works on arrays or "Traversable" objects, got "%s".', gettype($element)));
  }
  return self::addOrSetFilter($element, $at, $value);
}