public static function TwigExtension::addFilter in Components! 3.x
Same name and namespace in other branches
- 8.2 src/Template/TwigExtension.php \Drupal\components\Template\TwigExtension::addFilter()
Adds a deeply-nested property on an array.
If the deeply-nested property exists, the existing data will be replaced with the new value, unless the existing data is an array. In which case, the new value will be merged into the existing array.
{{ form|add( 'element.#attributes.class', 'new-class' ) }}
Or using named arguments:
{{ form|add( to='element.#attributes.class', value='new-class' ) }}
{# We accept the plural form of "values" as a grammatical convenience. #}
{{ form|add( to='element.#attributes.class', values=['new-class', 'new-class-2'] ) }}
Parameters
array|iterable|\Traversable $element: The parent renderable array to merge into.
string $at: The dotted-path to the deeply nested element to modify.
mixed $value: The value to add.
mixed $values: The values to add. If this named argument is used, the "value" argument is ignored.
Return value
array The merged renderable array.
Throws
\Twig\Error\RuntimeError When $element is not an array or "Traversable".
2 calls to TwigExtension::addFilter()
- TwigExtensionFiltersTest::testAddFilter in tests/
src/ Unit/ TwigExtensionFiltersTest.php - Tests the add filter.
- TwigExtensionFiltersTest::testAddFilterException in tests/
src/ Unit/ TwigExtensionFiltersTest.php - Tests exceptions during add filter.
File
- src/
Template/ TwigExtension.php, line 199
Class
- TwigExtension
- A class providing components' Twig extensions.
Namespace
Drupal\components\TemplateCode
public static function addFilter($element, string $at, $value = NULL, $values = NULL) {
if (!twig_test_iterable($element)) {
throw new RuntimeError(sprintf('The add filter only works on arrays or "Traversable" objects, got "%s".', gettype($element)));
}
return self::addOrSetFilter($element, $at, !is_null($values) ? $values : $value, TRUE);
}