View source  
  <?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
class Table extends WebformElementBase {
  
  protected function defineDefaultProperties() {
    return [
      
      'header' => [],
      'empty' => '',
    ];
  }
  
  protected function defineTranslatableProperties() {
    return array_merge(parent::defineTranslatableProperties(), [
      'header',
    ]);
  }
  
  
  public function isInput(array $element) {
    return FALSE;
  }
  
  public function isContainer(array $element) {
    return TRUE;
  }
  
  public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
    parent::prepare($element, $webform_submission);
    
    $element['#attributes']['class'][] = 'js-form-wrapper';
    
    $element['#tree'] = FALSE;
  }
  
  public function getItemDefaultFormat() {
    return 'table';
  }
  
  public function getItemFormats() {
    return [
      'table',
    ];
  }
  
  public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
    
    return NULL;
  }
  
  protected function format($type, array &$element, WebformSubmissionInterface $webform_submission, array $options = []) {
    $item_function = 'format' . $type . 'Item';
    return $this
      ->{$item_function}($element, $webform_submission, $options);
  }
  
  protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
    $rows = [];
    foreach ($element as $row_key => $row_element) {
      if (WebformElementHelper::property($row_key)) {
        continue;
      }
      $element[$row_key] = [];
      foreach ($row_element as $column_key => $column_element) {
        if (WebformElementHelper::property($column_key)) {
          continue;
        }
        
        $column_element_plugin = $this->elementManager
          ->getElementInstance($column_element);
        $column_value = $column_element_plugin
          ->format('html', $column_element, $webform_submission, $options);
        
        if (empty($column_value) && isset($column_element['#markup'])) {
          $column_value = $column_element['#markup'];
        }
        if (is_array($column_value)) {
          $rows[$row_key][$column_key] = [
            'data' => $column_value,
          ];
        }
        else {
          $rows[$row_key][$column_key] = [
            'data' => [
              '#markup' => $column_value,
            ],
          ];
        }
      }
    }
    return $rows + $element;
  }
  
  protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
    
    $build = $this
      ->formatHtml($element, $webform_submission, $options);
    $html = \Drupal::service('renderer')
      ->renderPlain($build);
    
    $html = preg_replace('#\\s*</td>\\s*<td[^>]*>\\s*#', ' | ', $html);
    $html = preg_replace('#\\s*</th>\\s*<th[^>]*>\\s*#', ' | ', $html);
    $html = preg_replace('#^\\s+#m', '', $html);
    $html = preg_replace('#\\s+$#m', '', $html);
    $html = preg_replace('#\\n+#s', PHP_EOL, $html);
    $html = strip_tags($html);
    
    $html = preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", PHP_EOL, $html);
    
    if (!empty($element['#header'])) {
      $lines = explode(PHP_EOL, trim($html));
      $lines[0] .= PHP_EOL . str_repeat('-', mb_strlen($lines[0]));
      $html = implode(PHP_EOL, $lines);
    }
    return $html;
  }
  
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['table'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Table settings'),
    ];
    $form['table']['header'] = [
      '#title' => $this
        ->t('Header (YAML)'),
      '#type' => 'webform_codemirror',
      '#mode' => 'yaml',
    ];
    $form['table']['empty'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Empty text'),
      '#description' => $this
        ->t('Text to display when no rows are present.'),
    ];
    
    unset($form['form']['size_container']['rows']);
    return $form;
  }
  
  public function getElementSelectorOptions(array $element) {
    return [];
  }
}