You are here

class TableTagPlugin in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x standard/src/Plugin/XBBCode/TableTagPlugin.php \Drupal\xbbcode_standard\Plugin\XBBCode\TableTagPlugin

Renders a table.

Plugin annotation


@XBBCodeTag(
  id = "table",
  label = @Translation("Table"),
  description = @Translation("Table with optional caption and header."),
  name = "table",
)

Hierarchy

Expanded class hierarchy of TableTagPlugin

File

standard/src/Plugin/XBBCode/TableTagPlugin.php, line 20

Namespace

Drupal\xbbcode_standard\Plugin\XBBCode
View source
class TableTagPlugin extends RenderTagPlugin {
  use TreeEncodeTrait;

  /**
   * The alignment indicators.
   */
  public const ALIGNMENT = [
    '~' => 'left',
    '!' => 'center',
    '#' => 'right',
  ];

  /**
   * {@inheritdoc}
   */
  public function buildElement(TagElementInterface $tag) : array {
    $element['#type'] = 'table';
    if ($caption = $tag
      ->getAttribute('caption')) {
      $element['#caption'] = $caption;
    }
    $alignments = [];
    if ($header = $tag
      ->getAttribute('header') ?: $tag
      ->getOption()) {

      /** @var string[] $headers */
      $headers = self::tabulateText($header)[0] ?: [
        $header,
      ];
      foreach ($headers as $i => $cell) {

        // Check if the label starts with an alignment symbol.
        if ($cell && array_key_exists($cell[0], self::ALIGNMENT)) {
          $alignments[$i] = self::ALIGNMENT[$cell[0]];
          $headers[$i] = substr($cell, 1);
        }
        else {

          // Trim leading whitespace.
          $headers[$i] = ltrim($cell);
          $alignments[$i] = NULL;
        }
      }
      if (implode('', $headers)) {
        $element['#header'] = $headers;
      }
    }
    foreach (static::tabulateTree($tag
      ->getChildren()) as $i => $row) {
      foreach ($row as $j => $cell) {
        $content = $cell
          ->getContent();

        // If not explicitly aligned, auto-align numeric strings.
        if (!isset($alignments[$j])) {
          $alignments[$j] = '';
        }
        $align = $alignments[$j] ?: (is_numeric($content) ? 'right' : NULL);
        $element["row-{$i}"][$j] = [
          '#markup' => Markup::create($content),
          '#wrapper_attributes' => $align ? [
            'style' => [
              'text-align:' . $align,
            ],
          ] : NULL,
        ];
      }
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultSample() : string {

    // Generate the sample here, as annotations don't do well with linebreaks.
    return $this
      ->t('[{{ name }} caption=Title header=!Item,Color,#Amount]
Fish,Red,1
Fish,Blue,2
[/{{ name }}]
[{{ name }}=~Left,Auto,!Center,#Right]
One,Two,Three,"Four, Five"
1,2,3,4
[/{{ name }}]
');
  }

  /**
   * Helper that turns a parse tree into a table of cells.
   *
   * @param array $children
   *   The parse tree under the table tag.
   *
   * @return \Drupal\xbbcode\Parser\Tree\TagElementInterface[][]
   *   Array of rows, each row an array of cells, each cell a node element.
   */
  private static function tabulateTree(array $children) : array {
    $table = [];
    [
      $token,
      $text,
    ] = static::encodeTree($children);
    foreach (self::tabulateText($text) as $i => $row) {
      foreach ($row as $j => $cell) {
        $table[$i][$j] = self::decodeTree($cell, $children, $token);
      }
    }
    return $table;
  }

  /**
   * Tabulate a text into lines and columns.
   *
   * @param string $text
   *   The text to tabulate.
   *
   * @return string[][]
   *   The tabulated array, or false if it is atomic.
   */
  private static function tabulateText($text) : array {

    // Trim, and strip linebreaks before newlines.
    $trimmed = preg_replace('/<br\\s*\\/?>\\n/', "\n", $text);
    $breaks = $trimmed !== $text;
    $text = trim($trimmed);

    // Tokenize on linebreaks and commas. Collapse multiple linebreaks.
    preg_match_all("/\n      (?:\n        (?'quote'['\"]|&quot;|&\\#039;)\n        (?'quoted'\n          (?:\\\\.|(?!\\\\|\\k'quote')[^\\\\])*\n        )\n        \\k'quote'\n        |\n        (?'unquoted'\n          (?:\\\\.|[^\\\\,\\v])*\n        )\n      )\n      (?'delimiter',|\\v+|\$)\n      /x", $text, $match, PREG_SET_ORDER);
    array_pop($match);
    $rows = [];
    $row = [];
    foreach ((array) $match as $token) {
      $value = stripslashes($token['quoted'] ?: $token['unquoted']);

      // Reinsert HTML linebreaks, if we removed them.
      if ($breaks) {
        $value = nl2br($value);
      }
      $row[] = $value;

      // Unless it is a column delimiter, end the row.
      if ($token['delimiter'] !== ',') {
        $rows[] = $row;
        $row = [];
      }
    }
    return $rows;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RenderTagPlugin::$renderer protected property The Drupal renderer.
RenderTagPlugin::create public static function Create a plugin instance. Overrides ContainerFactoryPluginInterface::create
RenderTagPlugin::doProcess public function Create the actual output. Overrides TagPluginBase::doProcess
RenderTagPlugin::__construct public function RenderTagPlugin constructor. Overrides TagPluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TableTagPlugin::ALIGNMENT public constant The alignment indicators.
TableTagPlugin::buildElement public function Build a render array from the tag. Overrides RenderTagPlugin::buildElement
TableTagPlugin::getDefaultSample public function Return the unprocessed sample code. Overrides TagPluginBase::getDefaultSample
TableTagPlugin::tabulateText private static function Tabulate a text into lines and columns.
TableTagPlugin::tabulateTree private static function Helper that turns a parse tree into a table of cells.
TagPluginBase::$name protected property The configurable tag name.
TagPluginBase::$sample protected property The sample code of this tag.
TagPluginBase::$settings protected property The settings for this tag plugin.
TagPluginBase::$status protected property A Boolean indicating whether this tag is enabled.
TagPluginBase::defaultConfiguration public function Get default plugin configuration from definition.
TagPluginBase::getConfiguration public function Get the plugin configuration.
TagPluginBase::getDefaultName public function Returns the default tag name. Overrides TagPluginInterface::getDefaultName
TagPluginBase::getDescription public function Returns the administrative description for this tag plugin. Overrides TagPluginInterface::getDescription
TagPluginBase::getName public function Returns the configured name. Overrides TagPluginInterface::getName
TagPluginBase::getSample public function Return a sample tag for the filter tips. Overrides TagPluginInterface::getSample
TagPluginBase::label public function Returns the administrative label for this tag plugin. Overrides TagPluginInterface::label
TagPluginBase::prepare public function Transform an elements' content, to armor against other filters. Overrides TagPluginInterface::prepare 2
TagPluginBase::process public function Generate output from a tag element. Overrides TagPluginInterface::process
TagPluginBase::setConfiguration public function Set the plugin configuration after instancing.
TagPluginBase::status public function Returns the status of this tag plugin. Overrides TagPluginInterface::status
TreeEncodeTrait::decodeTree protected static function Decode a part of the encoded tree.
TreeEncodeTrait::encodeTree protected static function Concatenate the top-level text of the tree.