You are here

public static function TwigExtension::setFilter in Components! 8.2

Same name and namespace in other branches
  1. 3.x 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|iterable|array $at: The dotted-path to the deeply nested element to set. (Or an array value to merge, if using the backwards-compatible 2.x syntax.)

mixed $value: The value to set.

string $path: The deprecated named argument that has been replaced with "at".

iterable|array $array: The deprecated named argument for the backwards-compatible 2.x syntax.

Return value

array The merged renderable array.

Throws

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

3 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.
TwigExtensionFiltersTest::testSetFilterMissingArgumentException in tests/src/Unit/TwigExtensionFiltersTest.php
Tests exceptions during set filter.

File

src/Template/TwigExtension.php, line 167

Class

TwigExtension
A class providing components' Twig extensions.

Namespace

Drupal\components\Template

Code

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

  // Backwards-compatibility with older 8.x-2.x versions of set filter.
  if (!is_null($array)) {
    $at = $array;
  }
  if (is_null($path) && is_null($at)) {
    throw new RuntimeError('Value for argument "at" is required for filter "set".');
  }
  if (!is_null($path)) {
    @trigger_error('The "set" 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;
  }
  if (!is_string($at)) {
    @trigger_error('Calling the "set" filter with an array is deprecated in components:8.x-2.3 and will be removed in components:3.0.0. Update to the new syntax or use the "recursive_merge" filter instead. See https://www.drupal.org/project/components/issues/3209440', E_USER_DEPRECATED);
    return self::recursiveMergeFilter($element, $at);
  }
  return self::addOrSetFilter($element, $at, $value, FALSE);
}