You are here

function xbbcode_basic_render_table in Extensible BBCode 8.2

Renders a table tag.

1 string reference to 'xbbcode_basic_render_table'
xbbcode_basic_xbbcode_info in xbbcode_basic/xbbcode_basic.module

File

xbbcode_basic/xbbcode_basic.module, line 289

Code

function xbbcode_basic_render_table($tag) {
  $alignment = [
    '' => 'left',
    '#' => 'right',
    '!' => 'center',
  ];
  $table = [
    '#type' => 'table',
    '#rows' => [],
  ];
  if ($tag->option) {

    // Look for an optional caption;header syntax.
    preg_match('/^(?:(.*);)?(.*)$/', $tag->option, $match);
    list($_, $table['#caption'], $headers) = $match;
  }
  else {
    $table['#caption'] = $tag
      ->attr('caption');
    $headers = $tag
      ->attr('headers');
  }
  if ($headers) {
    $headers = explode(',', $headers);
    $table['#header'] = [];
    foreach ($headers as $i => $header) {

      // !<name> is aligned right, #<name> is centered, otherwise aligned left.
      preg_match('/^([#!]?)(.+)$/', $header, $match);
      $table['#header'][$i] = $match[2];
      $align[$i] = $alignment[$match[1]];
    }
  }
  $rows = explode("\n", trim($tag->content));
  foreach ($rows as $i => $row) {
    $row = preg_split('/(?<!\\\\),/', $row);
    if ($headers) {
      foreach ($row as $j => $cell) {
        $row[$j] = [
          '#markup' => trim(str_replace('\\,', ',', $cell)),
          '#wrapper_attributes' => [
            'style' => 'text-align:' . $align[$j],
          ],
        ];
      }
      $table["row-{$i}"] = $row;
    }
    else {
      $table['#rows'][] = $row;
    }
  }

  // Strip trailing intertag whitespace, in case this text passes through a linebreak converter.
  return preg_replace('/(>)\\s+|\\s+(<)/', '\\1\\2', drupal_render($table));
}