You are here

public function WebformTableTrait::prepare in Webform 8.5

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

File

src/Plugin/WebformElement/WebformTableTrait.php, line 18

Class

WebformTableTrait
Provides a 'table' trait.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Add missing element class.
  $element['#attributes']['class'][] = str_replace('_', '-', $element['#type']);

  // If the #header is not specified, use the element's title as
  // the column header.
  if (!isset($element['#header'])) {
    if (empty($element['#title_display']) || $element['#title_display'] === 'header') {
      $element['#header'] = [
        [
          'data' => static::buildElementTitle($element),
        ],
      ];
    }
    else {
      $element['#header'] = [
        '',
      ];
    }
  }

  // By default do not display the table element's title.
  if (empty($element['#title_display']) || $element['#title_display'] === 'header') {
    $element['#title_display'] = 'none';
  }

  // Convert associative array of options into one column row.
  if (isset($element['#options'])) {
    foreach ($element['#options'] as $options_key => $options_value) {
      if (is_string($options_value)) {
        $element['#options'][$options_key] = [
          [
            'value' => $options_value,
          ],
        ];
      }
    }
  }
  $element['#attached']['library'][] = 'webform/webform.element.' . $element['#type'];

  // Set table select element's #process callback so that fix UX
  // and accessibility issues.
  if ($this
    ->getPluginId() === 'tableselect') {
    static::setProcessTableSelectCallback($element);
  }

  // Add form element theme wrapper.
  $element['#theme_wrappers'][] = 'form_element';
  $element['#label_attributes']['webform-remove-for-attribute'] = TRUE;
}