protected static function TwigExtension::addOrSetFilter in Components! 3.x
Same name and namespace in other branches
- 8.2 src/Template/TwigExtension.php \Drupal\components\Template\TwigExtension::addOrSetFilter()
Helper function for the set/add filters.
Parameters
array|iterable|\Traversable $element: The parent renderable array to merge into.
string $at: The dotted-path to the deeply nested element to replace.
mixed $value: The value to set.
bool $isAddFilter: Which filter is being called.
Return value
array The merged renderable array.
2 calls to TwigExtension::addOrSetFilter()
- TwigExtension::addFilter in src/
Template/ TwigExtension.php - Adds a deeply-nested property on an array.
- TwigExtension::setFilter in src/
Template/ TwigExtension.php - Sets a deeply-nested property on an array.
File
- src/
Template/ TwigExtension.php, line 222
Class
- TwigExtension
- A class providing components' Twig extensions.
Namespace
Drupal\components\TemplateCode
protected static function addOrSetFilter($element, string $at, $value, $isAddFilter = FALSE) {
if ($element instanceof \ArrayAccess) {
$filteredElement = clone $element;
}
else {
$filteredElement = $element;
}
// Convert the dotted path into an array of keys.
$path = explode('.', $at);
$lastPath = array_pop($path);
// Traverse the element down the path, creating arrays as needed.
$childElement =& $filteredElement;
foreach ($path as $childPath) {
if (!isset($childElement[$childPath])) {
$childElement[$childPath] = [];
}
$childElement =& $childElement[$childPath];
}
// If this is the add() filter and if the targeted child element is an
// array, add the value to it.
if ($isAddFilter && isset($childElement[$lastPath]) && is_array($childElement[$lastPath])) {
if (is_array($value)) {
$childElement[$lastPath] = array_merge($childElement[$lastPath], $value);
}
else {
$childElement[$lastPath][] = $value;
}
}
else {
// Otherwise, replace the target element with the given value.
$childElement[$lastPath] = $value;
}
return $filteredElement;
}