You are here

public static function ElementTree::applyRecursively in Little helpers 7.2

Apply a callback recursively to all elements in a form or renderable array.

The element tree is traversed using depth-first-search. For example: | root | | fieldset1 | | | textfield1 | | textfield2

This works similar to array_walk_recursive() with two differences:

  • It uses element_children() to find child elements.
  • Additional context can be bound to the $callback closure.

Parameters

array $element: The root of the tree that should be worked on.

callable $callback: The function that‘s applied recursively. It must accept two arguments:

  • &$element: The reference to the element.
  • $key: The element’s key in the parent array or NULL for the root.
  • &$parent: The parent element or NULL for the root.

bool $post_order: Use a post-order instead of a pre-order traversal.

2 calls to ElementTree::applyRecursively()
ElementTreeTest::testApplyRecursivelyPostOrder in tests/ElementTreeTest.php
Test post-order traversal.
ElementTreeTest::testApplyRecursivelyPreOrder in tests/ElementTreeTest.php
Test reading and modifying the element tree.

File

src/ElementTree.php, line 33

Class

ElementTree
A collection of helper function for element trees.

Namespace

Drupal\little_helpers

Code

public static function applyRecursively(array &$element, callable $callback, $post_order = FALSE) {
  if ($post_order) {
    static::applyRecursivelyPostOrder($element, $callback);
  }
  else {
    static::applyRecursivelyPreOrder($element, $callback);
  }
}