You are here

protected function WebformElementBase::prepareMultipleWrapper in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::prepareMultipleWrapper()

Set multiple element wrapper.

Parameters

array $element: An element.

2 calls to WebformElementBase::prepareMultipleWrapper()
WebformCompositeBase::prepareMultipleWrapper in src/Plugin/WebformElement/WebformCompositeBase.php
Set multiple element wrapper.
WebformElementBase::finalize in src/Plugin/WebformElementBase.php
Finalize an element to be rendered within a webform.
1 method overrides WebformElementBase::prepareMultipleWrapper()
WebformCompositeBase::prepareMultipleWrapper in src/Plugin/WebformElement/WebformCompositeBase.php
Set multiple element wrapper.

File

src/Plugin/WebformElementBase.php, line 1198

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

protected function prepareMultipleWrapper(array &$element) {
  if (!$this
    ->hasMultipleValues($element) || !$this
    ->hasMultipleWrapper() || empty($element['#multiple'])) {
    return;
  }

  // Set the multiple element.
  $element['#element'] = $element;

  // Remove properties that should only be applied to the parent element.
  $element['#element'] = array_diff_key($element['#element'], array_flip([
    '#access',
    '#default_value',
    '#description',
    '#description_display',
    '#required',
    '#required_error',
    '#states',
    '#wrapper_attributes',
    '#prefix',
    '#suffix',
    '#element',
    '#tags',
    '#multiple',
  ]));

  // Propagate #states to sub element.
  // @see \Drupal\webform\Element\WebformCompositeBase::processWebformComposite
  if (!empty($element['#states'])) {
    $element['#element']['#_webform_states'] = $element['#states'];
  }

  // Always make the title invisible.
  $element['#element']['#title_display'] = 'invisible';

  // Set hidden element #after_build handler.
  $element['#element']['#after_build'][] = [
    get_class($this),
    'hiddenElementAfterBuild',
  ];

  // Remove 'for' from the main element's label.
  // This must be done after the $element['#element' is defined.
  $element['#label_attributes']['webform-remove-for-attribute'] = TRUE;

  // Change the element to a multiple element.
  $element['#type'] = 'webform_multiple';
  $element['#webform_multiple'] = TRUE;

  // Set cardinality from #multiple.
  if ($element['#multiple'] > 1) {
    $element['#cardinality'] = $element['#multiple'];
  }

  // Apply multiple properties.
  $multiple_properties = $this
    ->defineDefaultMultipleProperties();
  foreach ($multiple_properties as $multiple_property => $multiple_value) {
    if (strpos($multiple_property, 'multiple__') === 0) {
      $property_name = str_replace('multiple__', '', $multiple_property);
      $element["#{$property_name}"] = isset($element["#{$multiple_property}"]) ? $element["#{$multiple_property}"] : $multiple_value;
    }
  }

  // If header label is defined use it for the #header.
  if (!empty($element['#multiple__header_label'])) {
    $element['#header'] = $element['#multiple__header_label'];
  }

  // Remove properties that should only be applied to the child element.
  $element = array_diff_key($element, array_flip([
    '#attributes',
    '#field_prefix',
    '#field_suffix',
    '#pattern',
    '#placeholder',
    '#maxlength',
    '#element_validate',
    '#pre_render',
  ]));

  // Apply #unique multiple validation.
  if (isset($element['#unique'])) {
    $element['#element_validate'][] = [
      get_class($this),
      'validateUniqueMultiple',
    ];
  }
}