You are here

protected static function WebformMultiple::buildElementHeader in Webform 8.5

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

Build a single element header.

Parameters

array $element: The element.

Return value

array A render array containing inputs for an element's header.

1 call to WebformMultiple::buildElementHeader()
WebformMultiple::processWebformMultiple in src/Element/WebformMultiple.php
Process items and build multiple elements widget.

File

src/Element/WebformMultiple.php, line 413

Class

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

Namespace

Drupal\webform\Element

Code

protected static function buildElementHeader(array $element) {
  $table_id = implode('-', $element['#parents']) . '-table';
  $colspan = 0;
  if ($element['#sorting']) {
    $colspan += 3;
  }
  if ($element['#operations']) {
    $colspan += 1;
  }
  if (empty($element['#header'])) {
    return [
      [
        'data' => !empty($element['#title']) ? WebformAccessibilityHelper::buildVisuallyHidden($element['#title']) : [],
        'colspan' => $colspan + 1,
      ],
    ];
  }
  elseif (is_array($element['#header'])) {
    $header = [];
    if ($element['#sorting']) {
      $header[] = [
        'data' => WebformAccessibilityHelper::buildVisuallyHidden(t('Re-order')),
        'class' => [
          "{$table_id}--handle",
          'webform-multiple-table--handle',
        ],
      ];
    }
    $header = array_merge($header, $element['#header']);
    if ($element['#sorting']) {
      $header[] = [
        'data' => [
          '#markup' => t('Weight'),
        ],
        'class' => [
          "{$table_id}--weight",
          'webform-multiple-table--weight',
        ],
      ];
    }
    if ($element['#operations']) {
      $header[] = [
        'data' => WebformAccessibilityHelper::buildVisuallyHidden(t('Operations')),
        'class' => [
          "{$table_id}--handle",
          'webform-multiple-table--operations',
        ],
      ];
    }
    return $header;
  }
  elseif (is_string($element['#header'])) {
    return [
      [
        'data' => $element['#header'],
        'colspan' => $element['#child_keys'] ? count($element['#child_keys']) + $colspan : $colspan + 1,
      ],
    ];
  }
  else {
    $header = [];
    if ($element['#sorting']) {
      $header['_handle_'] = [
        'data' => WebformAccessibilityHelper::buildVisuallyHidden(t('Re-order')),
        'class' => [
          "{$table_id}--handle",
          "webform-multiple-table--handle",
        ],
      ];
    }
    if ($element['#child_keys']) {
      foreach ($element['#child_keys'] as $child_key) {
        if (static::isHidden($element['#element'][$child_key])) {
          continue;
        }
        $child_element = $element['#element'][$child_key];

        // Build element title.
        $header[$child_key] = [
          'data' => static::buildElementTitle($child_element),
        ];

        // Append label attributes to header.
        if (!empty($child_element['#label_attributes'])) {
          $header[$child_key] += $child_element['#label_attributes'];
        }
        $header[$child_key]['class'][] = "{$table_id}--{$child_key}";
        $header[$child_key]['class'][] = "webform-multiple-table--{$child_key}";
      }
    }
    else {
      $header['item'] = [
        'data' => isset($element['#element']['#title']) ? $element['#element']['#title'] : '',
        'class' => [
          "{$table_id}--item",
          "webform-multiple-table--item",
        ],
      ];
    }
    if ($element['#sorting']) {
      $header['weight'] = [
        'data' => t('Weight'),
        'class' => [
          "{$table_id}--weight",
          "webform-multiple-table--weight",
        ],
      ];
    }
    if ($element['#operations']) {
      $header['_operations_'] = [
        'data' => WebformAccessibilityHelper::buildVisuallyHidden(t('Operations')),
        'class' => [
          "{$table_id}--operations",
          "webform-multiple-table--operations",
        ],
      ];
    }
    return $header;
  }
}