You are here

protected function Table::formatTextItem in Webform 6.x

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

Format an element's value as text.

Parameters

array $element: An element.

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

array $options: An array of options.

Return value

string The element's value formatted as text.

Overrides WebformElementBase::formatTextItem

See also

_webform_token_get_submission_value()

File

src/Plugin/WebformElement/Table.php, line 159

Class

Table
Provides a 'table' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {

  // Render the HTML table.
  $build = $this
    ->formatHtml($element, $webform_submission, $options);
  $html = $this->renderer
    ->renderPlain($build);

  // Convert table in pipe delimited plain text.
  $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);

  // Remove blank links from text.
  // From: http://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php
  $html = preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", PHP_EOL, $html);

  // Add divider between (optional) header.
  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;
}