You are here

public function TwigExtension::withoutFilter in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::withoutFilter()

Removes child elements from a copy of the original array.

Creates a copy of the renderable array and removes child elements by key specified through filter's arguments. The copy can be printed without these elements. The original renderable array is still available and can be used to print child elements in their entirety in the twig template.

Parameters

array|object $element: The parent renderable array to exclude the child items.

string[]|string ...: The string keys of $element to prevent printing. Arguments can include string keys directly, or arrays of string keys to hide.

Return value

array The filtered renderable array.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 661

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function withoutFilter($element) {
  if ($element instanceof \ArrayAccess) {
    $filtered_element = clone $element;
  }
  else {
    $filtered_element = $element;
  }
  $args = func_get_args();
  unset($args[0]);

  // Since the remaining arguments can be a mix of arrays and strings, we use
  // some native PHP iterator classes to allow us to recursively iterate over
  // everything in a single pass.
  $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($args));
  foreach ($iterator as $key) {
    unset($filtered_element[$key]);
  }
  return $filtered_element;
}