You are here

protected static function WebformCompositeBase::initializeCompositeElementsRecursive in Webform 6.x

Same name in this branch
  1. 6.x src/Element/WebformCompositeBase.php \Drupal\webform\Element\WebformCompositeBase::initializeCompositeElementsRecursive()
  2. 6.x src/Plugin/WebformElement/WebformCompositeBase.php \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::initializeCompositeElementsRecursive()
Same name and namespace in other branches
  1. 8.5 src/Element/WebformCompositeBase.php \Drupal\webform\Element\WebformCompositeBase::initializeCompositeElementsRecursive()

Initialize a composite's elements recursively.

Parameters

array $element: A render array for the current element.

array $composite_elements: A render array containing a composite's elements.

Throws

\Exception Throws exception when unsupported element type is used with a composite element.

1 call to WebformCompositeBase::initializeCompositeElementsRecursive()
WebformCompositeBase::initializeCompositeElements in src/Element/WebformCompositeBase.php
Initialize a composite's elements.

File

src/Element/WebformCompositeBase.php, line 226

Class

WebformCompositeBase
Provides an base composite webform element.

Namespace

Drupal\webform\Element

Code

protected static function initializeCompositeElementsRecursive(array &$element, array &$composite_elements) {

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  foreach ($composite_elements as $composite_key => &$composite_element) {
    if (WebformElementHelper::property($composite_key)) {
      continue;
    }

    // Transfer '#{composite_key}_{property}' from main element to composite
    // element.
    foreach ($element as $property_key => $property_value) {
      if (strpos($property_key, '#' . $composite_key . '__') === 0) {
        $composite_property_key = str_replace('#' . $composite_key . '__', '#', $property_key);
        $composite_element[$composite_property_key] = $property_value;
      }
    }

    // Initialize composite sub-element.
    $element_plugin = $element_manager
      ->getElementInstance($composite_element);

    // Make sure to remove any #options references from unsupported elements.
    // This prevents "An illegal choice has been detected." error.
    // @see FormValidator::performRequiredValidation()
    if (isset($composite_element['#options']) && !$element_plugin
      ->hasProperty('options')) {
      unset($composite_element['#options']);
    }

    // Convert #placeholder to #empty_option for select elements.
    if (isset($composite_element['#placeholder']) && $element_plugin
      ->hasProperty('empty_option')) {
      $composite_element['#empty_option'] = $composite_element['#placeholder'];
    }

    // Apply #select2, #choices, and #chosen to select elements.
    if (isset($composite_element['#type']) && strpos($composite_element['#type'], 'select') !== FALSE) {
      $select_properties = [
        '#select2' => '#select2',
        '#choices' => '#choices',
        '#chosen' => '#chosen',
      ];
      $composite_element += array_intersect_key($element, $select_properties);
    }
    if ($element_plugin
      ->hasMultipleValues($composite_element)) {
      throw new \Exception('Multiple elements are not supported within composite elements.');
    }
    if ($element_plugin
      ->isComposite()) {
      throw new \Exception('Nested composite elements are not supported within composite elements.');
    }
    $element_plugin
      ->initialize($composite_element);
    static::initializeCompositeElementsRecursive($element, $composite_element);
  }
}