View source
<?php
namespace Drupal\xbbcode;
use Drupal;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\xbbcode\Parser\XBBCodeParser;
class TagPluginCollection extends DefaultLazyPluginCollection implements PluginCollectionInterface {
use PluginCollectionArrayAdapter;
use StringTranslationTrait;
public function __construct(TagPluginManager $manager, array $configurations = []) {
parent::__construct($manager, $configurations);
$this
->setConfiguration($configurations);
$this
->sort();
}
public static function createFromTags(array $tags) {
$configurations = [];
foreach ($tags as $name => $tag) {
$configurations[$name]['id'] = $tag
->getPluginId();
}
$collection = new static(Drupal::service('plugin.manager.xbbcode'), $configurations);
$collection->pluginInstances = $tags;
return $collection;
}
public function setConfiguration($configuration) {
foreach ($configuration as $instance_id => $plugin) {
$configuration[$instance_id]['name'] = $instance_id;
}
parent::setConfiguration($configuration);
}
public function getConfiguration() : array {
$configuration = parent::getConfiguration();
$original = [];
foreach ($configuration as $instance_id => $plugin) {
$name = $plugin['name'];
unset($plugin['name']);
$original[$name] = $plugin;
}
return $original;
}
public function sortHelper($a, $b) {
return strnatcasecmp($a, $b);
}
public function getSummary() : array {
$tags = [
'#theme' => 'item_list',
'#context' => [
'list_style' => 'comma-list',
],
'#items' => [],
'#empty' => $this
->t('None'),
'#attached' => [
'library' => [
'xbbcode/filter-tips',
],
],
'#attributes' => [
'class' => [
'xbbcode-filter-tips',
],
],
];
foreach ($this as $name => $tag) {
$tags['#items'][$name] = [
'#type' => 'inline_template',
'#template' => '<abbr title="{{ tag.description }}">[{{ tag.name }}]</abbr>',
'#context' => [
'tag' => $tag,
],
];
}
return $tags;
}
public function getTable() : array {
$table = [
'#type' => 'table',
'#caption' => $this
->t('Allowed BBCode tags:'),
'#header' => [
$this
->t('Tag Description'),
$this
->t('You Type'),
$this
->t('You Get'),
],
'#empty' => $this
->t('BBCode is active, but no tags are available.'),
];
foreach ($this as $name => $tag) {
$parser = new XBBCodeParser(static::createFromTags([
$name => $tag,
]));
$tree = $parser
->parse($tag
->getSample());
$sample = $tree
->render();
$attachments = [];
foreach ($tree
->getRenderedChildren() as $child) {
if ($child instanceof TagProcessResult) {
$attachments = BubbleableMetadata::mergeAttachments($attachments, $child
->getAttachments());
}
}
$table[$name] = [
[
'#type' => 'inline_template',
'#template' => '<strong>[{{ tag.name }}]</strong><br /> {{ tag.description }}',
'#context' => [
'tag' => $tag,
],
'#attributes' => [
'class' => [
'description',
],
],
],
[
'#type' => 'inline_template',
'#template' => '<code>{{ tag.sample|nl2br }}</code>',
'#context' => [
'tag' => $tag,
],
'#attributes' => [
'class' => [
'type',
],
],
],
[
'#markup' => Markup::create($sample),
'#attached' => $attachments,
'#attributes' => [
'class' => [
'get',
],
],
],
];
}
return $table;
}
public function has($instance_id) : bool {
return (bool) parent::has($instance_id);
}
}