public static function TwigExtension::addFilter in Components! 8.2
Same name and namespace in other branches
- 3.x 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.
string $path: The deprecated named argument that has been replaced with "at".
Return value
array The merged renderable array.
Throws
\Twig\Error\RuntimeError When $element is not an array or "Traversable".
3 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.
- TwigExtensionFiltersTest::testAddFilterMissingArgumentException in tests/
src/ Unit/ TwigExtensionFiltersTest.php - Tests exceptions during add filter.
File
- src/
Template/ TwigExtension.php, line 227
Class
- TwigExtension
- A class providing components' Twig extensions.
Namespace
Drupal\components\TemplateCode
public static function addFilter($element, string $at = NULL, $value = NULL, $values = NULL, $path = NULL) {
if (!twig_test_iterable($element)) {
throw new RuntimeError(sprintf('The add filter only works on arrays or "Traversable" objects, got "%s".', gettype($element)));
}
// Backwards-compatibility with older 8.x-2.x versions of add filter.
if (is_null($path) && is_null($at)) {
throw new RuntimeError('Value for argument "at" is required for filter "add".');
}
if (!is_null($path)) {
@trigger_error('The "add" filter’s named "path" argument is deprecated in components:8.x-2.4 and will be removed in components:3.0.0. The named argument has been renamed from "path" to "at". See https://www.drupal.org/project/components/issues/3209575', E_USER_DEPRECATED);
$at = $path;
}
return self::addOrSetFilter($element, $at, !is_null($values) ? $values : $value, TRUE);
}