You are here

public function WebformCustomComposite::prepare in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformCustomComposite.php \Drupal\webform\Plugin\WebformElement\WebformCustomComposite::prepare()

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides WebformCompositeBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

src/Plugin/WebformElement/WebformCustomComposite.php, line 63

Class

WebformCustomComposite
Provides a custom composite element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  parent::prepare($element, $webform_submission);

  // Set cardinality.
  if (isset($element['#multiple'])) {
    $element['#cardinality'] = $element['#multiple'] === FALSE ? 1 : $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;
    }
  }

  // Default to displaying table header and label.
  $element += [
    '#header' => TRUE,
  ];

  // Transfer '#{composite_key}_{property}' from main element to composite
  // element.
  foreach ($element['#element'] as $composite_key => $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);
        $element['#element'][$composite_key][$composite_property_key] = $property_value;
      }
    }
  }
}