You are here

protected function WebformCompositeBase::formatHtmlItems in Webform 6.x

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

Format an element's items as HTML.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

string|array The element's items as HTML.

Overrides WebformElementBase::formatHtmlItems

File

src/Plugin/WebformElement/WebformCompositeBase.php, line 429

Class

WebformCompositeBase
Provides a base for composite elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatHtmlItems(array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $format = $this
    ->getItemsFormat($element);
  if ($format !== 'table') {
    return parent::formatHtmlItems($element, $webform_submission, $options);
  }
  $composite_elements = $this
    ->getInitializedCompositeElement($element);

  // Get header.
  $header = [];
  foreach (RenderElement::children($composite_elements) as $composite_key) {
    if (!Element::isVisibleElement($composite_elements[$composite_key])) {
      unset($composite_elements[$composite_key]);
      continue;
    }
    $composite_element = $composite_elements[$composite_key];
    $header[$composite_key] = [
      'data' => isset($composite_element['#title']) ? $composite_element['#title'] : $composite_key,
      'bgcolor' => '#eee',
    ];
  }

  // Get rows.
  $rows = [];
  $values = $this
    ->getValue($element, $webform_submission, $options);
  foreach ($values as $delta => $value) {
    foreach ($header as $composite_key => $composite_title) {
      $composite_value = $this
        ->formatCompositeHtml($element, $webform_submission, [
        'delta' => $delta,
        'composite_key' => $composite_key,
      ] + $options);
      if (is_array($composite_value)) {
        $rows[$delta][$composite_key] = [
          'data' => $composite_value,
        ];
      }
      else {
        $rows[$delta][$composite_key] = [
          'data' => [
            '#markup' => $composite_value,
          ],
        ];
      }
    }
  }
  return [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => [
      'width' => '100%',
      'cellspacing' => 0,
      'cellpadding' => 5,
      'border' => 1,
    ],
  ];
}