You are here

protected function YamlForm::initElementsRecursive in YAML Form 8

Initialize form elements into a flatten array.

Parameters

array $elements: The form elements.

1 call to YamlForm::initElementsRecursive()
YamlForm::initElements in src/Entity/YamlForm.php
Initialize parse form elements.

File

src/Entity/YamlForm.php, line 822

Class

YamlForm
Defines the form entity.

Namespace

Drupal\yamlform\Entity

Code

protected function initElementsRecursive(array &$elements, $parent = '', $depth = 0) {

  /** @var \Drupal\yamlform\YamlFormElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.yamlform.element');

  /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
  $element_info = \Drupal::service('plugin.manager.element_info');

  // Remove ignored properties.
  $elements = YamlFormElementHelper::removeIgnoredProperties($elements);
  foreach ($elements as $key => &$element) {
    if (Element::property($key) || !is_array($element)) {
      continue;
    }

    // Apply translation to element.
    if (isset($this->elementsTranslations[$key])) {
      YamlFormElementHelper::applyTranslation($element, $this->elementsTranslations[$key]);
    }

    // Copy only the element properties to decoded and flattened elements.
    $this->elementsDecodedAndFlattened[$key] = YamlFormElementHelper::getProperties($element);

    // Set id, key, parent_key, depth, and parent children.
    $element['#yamlform_id'] = $this
      ->id() . '--' . $key;
    $element['#yamlform_key'] = $key;
    $element['#yamlform_parent_key'] = $parent;
    $element['#yamlform_parent_flexbox'] = FALSE;
    $element['#yamlform_depth'] = $depth;
    $element['#yamlform_children'] = [];
    $element['#yamlform_multiple'] = FALSE;
    $element['#yamlform_composite'] = FALSE;
    if (!empty($parent)) {
      $parent_element = $this->elementsInitializedAndFlattened[$parent];

      // Add element to the parent element's children.
      $parent_element['#yamlform_children'][$key] = $key;

      // Set #parent_flexbox to TRUE is the parent element is a
      // 'yamlform_flexbox'.
      $element['#yamlform_parent_flexbox'] = isset($parent_element['#type']) && $parent_element['#type'] == 'yamlform_flexbox' ? TRUE : FALSE;
    }

    // Set #title and #admin_title to NULL if it is not defined.
    $element += [
      '#title' => NULL,
      '#admin_title' => NULL,
    ];

    // If #private set #access.
    if (!empty($element['#private'])) {
      $element['#access'] = $this
        ->access('submission_view_any');
    }
    $element_handler = NULL;
    if (isset($element['#type'])) {

      // Track managed file upload.
      if ($element['#type'] == 'managed_file') {
        $this->hasManagedFile = TRUE;
      }

      // Track flexbox.
      if ($element['#type'] == 'flexbox' || $element['#type'] == 'yamlform_flexbox') {
        $this->hasFlexboxLayout = TRUE;
      }

      // Set yamlform_* prefix to #type that are using alias without yamlform_
      // namespace.
      if (!$element_info
        ->hasDefinition($element['#type']) && $element_info
        ->hasDefinition('yamlform_' . $element['#type'])) {
        $element['#type'] = 'yamlform_' . $element['#type'];
      }

      // Load the element's handler.
      $element_handler = $element_manager
        ->createInstance($element['#type']);

      // Initialize the element.
      $element_handler
        ->initialize($element);
      $element['#yamlform_multiple'] = $element_handler
        ->hasMultipleValues($element);
      $element['#yamlform_composite'] = $element_handler
        ->isComposite();
    }

    // Copy only the element properties to initialized and flattened elements.
    $this->elementsInitializedAndFlattened[$key] = YamlFormElementHelper::getProperties($element);

    // Check if element has value (aka can be exported) and add it to
    // flattened has value array.
    if ($element_handler && $element_handler
      ->isInput($element)) {
      $this->elementsFlattenedAndHasValue[$key] =& $this->elementsInitializedAndFlattened[$key];
    }
    $this
      ->initElementsRecursive($element, $key, $depth + 1);
  }
}