You are here

protected static function WebformFormHelper::flattenElementsRecursive in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformFormHelper.php \Drupal\webform\Utility\WebformFormHelper::flattenElementsRecursive()

Traverse a render array and collect references to all elements in an associative array keyed by element key.

Parameters

array $build: An render array.

array $elements: An empty array that will be populated with references to all elements.

array $duplicate_element_keys: An array used to track elements with duplicate keys.

1 call to WebformFormHelper::flattenElementsRecursive()
WebformFormHelper::flattenElements in src/Utility/WebformFormHelper.php
Traverse a render array and collect references to all elements in an associative array keyed by element key.

File

src/Utility/WebformFormHelper.php, line 216

Class

WebformFormHelper
Helper class webform based methods.

Namespace

Drupal\webform\Utility

Code

protected static function flattenElementsRecursive(array &$build, array &$elements, array &$duplicate_element_keys) {
  foreach ($build as $key => &$element) {
    if (WebformElementHelper::isElement($element, $key)) {

      // If there are duplicate element keys create an array of referenced
      // elements.
      if (isset($elements[$key])) {

        // If this is the second element, we need to restructure to first
        // element's reference to be an array of references.
        if (empty($duplicate_element_keys[$key])) {

          // Store a temporary references to the first element.
          $first_element =& $elements[$key];

          // Use unset() to break the reference.
          unset($elements[$key]);

          // Create an array of element references.
          $elements[$key] = [];

          // Append the first to the array of element references.
          $elements[$key][] =& $first_element;
        }

        // Now append the current element to array of element references.
        $elements[$key][] =& $build[$key];

        // Finally track elements with duplicate keys.
        $duplicate_element_keys[$key] = TRUE;
      }
      else {
        $elements[$key] =& $build[$key];
      }
      self::flattenElementsRecursive($element, $elements, $duplicate_element_keys);
    }
  }
}