class ListTagPlugin in Extensible BBCode 8.3
Same name and namespace in other branches
- 4.0.x standard/src/Plugin/XBBCode/ListTagPlugin.php \Drupal\xbbcode_standard\Plugin\XBBCode\ListTagPlugin
Renders a list.
Plugin annotation
@XBBCodeTag(
id = "list",
label = @Translation("List"),
description = @Translation("List with optional style."),
name = "list",
)
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\ListTagPlugin 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 ListTagPlugin
File
- standard/
src/ Plugin/ XBBCode/ ListTagPlugin.php, line 22
Namespace
Drupal\xbbcode_standard\Plugin\XBBCodeView source
class ListTagPlugin extends RenderTagPlugin {
use TreeEncodeTrait;
/**
* {@inheritdoc}
*/
public function buildElement(TagElementInterface $tag) : array {
$element['#theme'] = 'item_list';
$style = $tag
->getOption() ?: $tag
->getAttribute('style');
[
$numbered,
$style,
] = static::validateStyle($style);
if ($numbered) {
$element['#list_type'] = 'ol';
}
if ($style) {
$element['#attributes']['style'] = 'list-style-type: ' . $style;
}
foreach (self::splitContent($tag
->getChildren()) as $i => $item) {
$element['#items'][$i] = Markup::create($item
->getContent());
}
return $element;
}
/**
* {@inheritdoc}
*/
public function getDefaultSample() : string {
return $this
->t('[{{ name }}=lower-roman]
[*] One
[*] Two
[*] Three
[/{{ name }}]');
}
/**
* Validate a style directive.
*
* @param string $style
* The user-entered style directive.
*
* @return array
* An array with two elements:
* - A boolean that is TRUE if the style is numbered, otherwise FALSE.
* - The style, if it is valid.
*/
protected static function validateStyle($style) : array {
// The predefined un-ordered styles.
if (in_array($style, [
'disc',
'circle',
'square',
'none',
], TRUE)) {
return [
FALSE,
$style,
];
}
// If the style contains no HTML characters, decode any character entities.
if (!preg_match('/\'"<>/', $style)) {
$style = Html::decodeEntities($style);
}
$style = trim($style);
// Match any quoted string.
if (preg_match('/
(?\'quote\'[\'"])
\\\\
(?:
[0-9a-fA-F]{1,6} # 1-6 hex digits preceded by a backslash.
|
[^0-9a-fA-F] # any other character preceded by a backslash.
)
|
(?!\\k\'quote\')[^\\\\] # any permissible non-backslash character.
\\k\'quote\'
/x', $style)) {
return [
FALSE,
$style,
];
}
// Match any expression.
if (preg_match('/
(?:
[^"\';] # anything other than quotes or semicolon.
|
\\\\
(?:
[0-9a-fA-F]{1,6} # 1-6 hex digits preceded by a backslash.
|
[^0-9a-fA-F] # any other character preceded by a backslash.
)
|
(?\'quote\'[\'"])
\\\\
(?:
[0-9a-fA-F]{1,6} # 1-6 hex digits preceded by a backslash.
|
[^0-9a-fA-F] # any other character preceded by a backslash.
)
|
(?!\\k\'quote\')[^\\\\] # any permissible non-backslash character.
\\k\'quote\'
)*
/x', $style)) {
return [
TRUE,
$style,
];
}
return [
FALSE,
'',
];
}
/**
* Split the tag's children into list items.
*
* Any instance of [*] in the top-level text will be used as a delimiter.
*
* @param \Drupal\xbbcode\Parser\Tree\ElementInterface[] $children
* The tag's child elements in the parse tree.
*
* @return \Drupal\xbbcode\Parser\Tree\NodeElementInterface[]
* A sequence of nodes, each containing a part of the parse tree.
*/
protected static function splitContent(array $children) : array {
[
$token,
$text,
] = static::encodeTree($children);
// Trim, and strip linebreaks before newlines.
$trimmed = preg_replace('/<br\\s*\\/?>\\n/', "\n", $text);
$breaks = $trimmed !== $text;
$text = trim($trimmed);
// Split on [*] at the start of lines.
$items = preg_split('/^\\s*\\[\\*]\\s*/m', $text);
array_shift($items);
foreach ($items as $i => $item) {
$item = trim($item);
if ($breaks) {
$item = nl2br($item);
}
$items[$i] = static::decodeTree($item, $children, $token);
}
return $items;
}
}
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 | |
ListTagPlugin:: |
public | function |
Build a render array from the tag. Overrides RenderTagPlugin:: |
|
ListTagPlugin:: |
public | function |
Return the unprocessed sample code. Overrides TagPluginBase:: |
|
ListTagPlugin:: |
protected static | function | Split the tag's children into list items. | |
ListTagPlugin:: |
protected static | function | Validate a style directive. | |
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. | |
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. |