You are here

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

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

Recursively merges an array into the element, replacing existing values.


{{ form|recursive_merge( {'element': {'attributes': {'placeholder': 'Label'}}} ) }}

Parameters

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

iterable|array $array: The array to merge.

Return value

array The merged renderable array.

Throws

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

2 calls to TwigExtension::recursiveMergeFilter()
TwigExtensionFiltersTest::testRecursiveMergeFilter in tests/src/Unit/TwigExtensionFiltersTest.php
Tests the recursive_merge filter.
TwigExtensionFiltersTest::testRecursiveMergeFilterException in tests/src/Unit/TwigExtensionFiltersTest.php
Tests exceptions during recursive_merge filter.

File

src/Template/TwigExtension.php, line 126

Class

TwigExtension
A class providing components' Twig extensions.

Namespace

Drupal\components\Template

Code

public static function recursiveMergeFilter($element, $array) : array {
  if (!twig_test_iterable($element)) {
    throw new RuntimeError(sprintf('The recursive_merge filter only works on arrays or "Traversable" objects, got "%s".', gettype($element)));
  }
  return array_replace_recursive($element, $array);
}