class TableTagPlugin in Extensible BBCode 8.3
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\xbbcode\Plugin\TagPluginBase implements TagPluginInterface
- class \Drupal\xbbcode\Plugin\RenderTagPlugin implements ContainerFactoryPluginInterface
- class \Drupal\xbbcode_standard\Plugin\XBBCode\TableTagPlugin uses TreeEncodeTrait
- class \Drupal\xbbcode\Plugin\RenderTagPlugin implements ContainerFactoryPluginInterface
- class \Drupal\xbbcode\Plugin\TagPluginBase implements TagPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TableTagPlugin
File
- standard/
src/ Plugin/ XBBCode/ TableTagPlugin.php, line 20
Namespace
Drupal\xbbcode_standard\Plugin\XBBCodeView 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'['\"]|"|&\\#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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RenderTagPlugin:: |
protected | property | The Drupal renderer. | |
RenderTagPlugin:: |
public static | function |
Create a plugin instance. Overrides ContainerFactoryPluginInterface:: |
|
RenderTagPlugin:: |
public | function |
Create the actual output. Overrides TagPluginBase:: |
|
RenderTagPlugin:: |
public | function |
RenderTagPlugin constructor. Overrides TagPluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TableTagPlugin:: |
public | constant | The alignment indicators. | |
TableTagPlugin:: |
public | function |
Build a render array from the tag. Overrides RenderTagPlugin:: |
|
TableTagPlugin:: |
public | function |
Return the unprocessed sample code. Overrides TagPluginBase:: |
|
TableTagPlugin:: |
private static | function | Tabulate a text into lines and columns. | |
TableTagPlugin:: |
private static | function | Helper that turns a parse tree into a table of cells. | |
TagPluginBase:: |
protected | property | The configurable tag name. | |
TagPluginBase:: |
protected | property | The sample code of this tag. | |
TagPluginBase:: |
protected | property | The settings for this tag plugin. | |
TagPluginBase:: |
protected | property | A Boolean indicating whether this tag is enabled. | |
TagPluginBase:: |
public | function | Get default plugin configuration from definition. | |
TagPluginBase:: |
public | function | Get the plugin configuration. | |
TagPluginBase:: |
public | function |
Returns the default tag name. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function |
Returns the administrative description for this tag plugin. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function |
Returns the configured name. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function |
Return a sample tag for the filter tips. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function |
Returns the administrative label for this tag plugin. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function |
Transform an elements' content, to armor against other filters. Overrides TagPluginInterface:: |
2 |
TagPluginBase:: |
public | function |
Generate output from a tag element. Overrides TagPluginInterface:: |
|
TagPluginBase:: |
public | function | Set the plugin configuration after instancing. | |
TagPluginBase:: |
public | function |
Returns the status of this tag plugin. Overrides TagPluginInterface:: |
|
TreeEncodeTrait:: |
protected static | function | Decode a part of the encoded tree. | |
TreeEncodeTrait:: |
protected static | function | Concatenate the top-level text of the tree. |