You are here

protected static function WebformMultiple::initializeElementRecursive in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformMultiple.php \Drupal\webform\Element\WebformMultiple::initializeElementRecursive()

Initialize, prepare, and finalize composite sub-elements recursively.

Parameters

array $element: The main element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: An associative array containing the structure of the form.

array $sub_elements: The sub element.

array $required_states: An associative array of required states from the main element's visible/hidden states.

1 call to WebformMultiple::initializeElementRecursive()
WebformMultiple::initializeElement in src/Element/WebformMultiple.php
Initialize element.

File

src/Element/WebformMultiple.php, line 350

Class

WebformMultiple
Provides a webform element to assist in creation of multiple elements.

Namespace

Drupal\webform\Element

Code

protected static function initializeElementRecursive(array $element, FormStateInterface $form_state, array &$complete_form, array &$sub_elements, array $required_states) {
  $child_keys = Element::children($sub_elements);

  // Exit immediate if the sub elements has no children.
  if (!$child_keys) {
    return;
  }

  // Determine if the sub elements are the main element for each table cell.
  $is_root = $element['#element'] === $sub_elements ? TRUE : FALSE;

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  foreach ($child_keys as $child_key) {
    $sub_element =& $sub_elements[$child_key];
    $element_plugin = $element_manager
      ->getElementInstance($sub_element);

    // If the element's #access is FALSE, apply it to all sub elements.
    if (isset($element['#access']) && $element['#access'] === FALSE) {
      $sub_element['#access'] = FALSE;
    }

    // If #header and root input then hide the sub element's #title.
    if ($element['#header'] && ($is_root && $element_plugin
      ->isInput($sub_element)) && !isset($sub_element['#title_display'])) {
      $sub_element['#title_display'] = 'invisible';
    }

    // Initialize the composite sub-element.
    $element_manager
      ->initializeElement($sub_element);

    // Build the composite sub-element.
    $element_manager
      ->buildElement($sub_element, $complete_form, $form_state);

    // Custom validate required sub-element because they can be hidden
    // via #access or #states.
    // @see \Drupal\webform\Element\WebformCompositeBase::validateWebformComposite
    if ($required_states && !empty($sub_element['#required'])) {
      unset($sub_element['#required']);
      $sub_element['#_required'] = TRUE;
      if (!isset($sub_element['#states'])) {
        $sub_element['#states'] = [];
      }
      $sub_element['#states'] += $required_states;
    }
    if (is_array($sub_element)) {
      static::initializeElementRecursive($element, $form_state, $complete_form, $sub_element, $required_states);
    }
  }
}