class TagPluginCollection in Extensible BBCode 8.3
Same name and namespace in other branches
- 4.0.x src/TagPluginCollection.php \Drupal\xbbcode\TagPluginCollection
A collection of tag plugins.
@property \Drupal\xbbcode\TagPluginManager manager
Hierarchy
- class \Drupal\Component\Plugin\LazyPluginCollection implements \Drupal\Component\Plugin\IteratorAggregate, \Drupal\Component\Plugin\Countable
- class \Drupal\Core\Plugin\DefaultLazyPluginCollection uses DependencySerializationTrait
- class \Drupal\xbbcode\TagPluginCollection implements PluginCollectionInterface uses StringTranslationTrait, PluginCollectionArrayAdapter
- class \Drupal\Core\Plugin\DefaultLazyPluginCollection uses DependencySerializationTrait
Expanded class hierarchy of TagPluginCollection
3 files declare their use of TagPluginCollection
- TagSet.php in src/
Entity/ TagSet.php - TagSetForm.php in src/
Form/ TagSetForm.php - TagSetInterface.php in src/
Entity/ TagSetInterface.php
File
- src/
TagPluginCollection.php, line 17
Namespace
Drupal\xbbcodeView source
class TagPluginCollection extends DefaultLazyPluginCollection implements PluginCollectionInterface {
use PluginCollectionArrayAdapter;
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function __construct(TagPluginManager $manager, array $configurations = []) {
parent::__construct($manager, $configurations);
$this
->setConfiguration($configurations);
$this
->sort();
}
/**
* Create a plugin collection directly from an array of tag plugins.
*
* @param \Drupal\xbbcode\Plugin\TagPluginInterface[] $tags
* The tag plugins.
*
* @return static
* A plugin collection.
*/
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;
}
/**
* {@inheritdoc}
*/
public function setConfiguration($configuration) {
// Copy instance ID into configuration as the tag name.
foreach ($configuration as $instance_id => $plugin) {
$configuration[$instance_id]['name'] = $instance_id;
}
parent::setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() : array {
// Strip tag name from configuration.
$configuration = parent::getConfiguration();
$original = [];
foreach ($configuration as $instance_id => $plugin) {
$name = $plugin['name'];
unset($plugin['name']);
$original[$name] = $plugin;
}
return $original;
}
/**
* {@inheritdoc}
*/
public function sortHelper($a, $b) {
// Sort by instance ID (which is the tag name) instead of plugin ID.
return strnatcasecmp($a, $b);
}
/**
* Generate a list of configured tags for display.
*
* @return array
* A render element.
*/
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;
}
/**
* Generate a table of available tags, with samples.
*
* @return array
* A render element.
*/
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) {
/** @var \Drupal\xbbcode\Plugin\TagPluginInterface $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;
}
/**
* {@inheritdoc}
*/
public function has($instance_id) : bool {
// This method is only overridden to hint the return type.
return (bool) parent::has($instance_id);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultLazyPluginCollection:: |
protected | property | The initial configuration for each plugin in the collection. | |
DefaultLazyPluginCollection:: |
protected | property | The manager used to instantiate the plugins. | |
DefaultLazyPluginCollection:: |
protected | property | The original order of the instances. | |
DefaultLazyPluginCollection:: |
protected | property | The key within the plugin configuration that contains the plugin ID. | 3 |
DefaultLazyPluginCollection:: |
public | function |
Adds an instance ID to the available instance IDs. Overrides LazyPluginCollection:: |
|
DefaultLazyPluginCollection:: |
protected | function |
Initializes and stores a plugin. Overrides LazyPluginCollection:: |
2 |
DefaultLazyPluginCollection:: |
public | function |
Removes an instance ID. Overrides LazyPluginCollection:: |
|
DefaultLazyPluginCollection:: |
public | function | Updates the configuration for a plugin instance. | |
DefaultLazyPluginCollection:: |
public | function | Sorts all plugin instances in this collection. | 1 |
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 | |
LazyPluginCollection:: |
protected | property | Stores the IDs of all potential plugin instances. | |
LazyPluginCollection:: |
protected | property | Stores all instantiated plugins. | |
LazyPluginCollection:: |
public | function | Clears all instantiated plugins. | |
LazyPluginCollection:: |
public | function | ||
LazyPluginCollection:: |
public | function | Gets a plugin instance, initializing it if necessary. | 4 |
LazyPluginCollection:: |
public | function | Gets all instance IDs. | |
LazyPluginCollection:: |
public | function | ||
LazyPluginCollection:: |
public | function | Removes an initialized plugin. | |
LazyPluginCollection:: |
public | function | Stores an initialized plugin. | |
PluginCollectionArrayAdapter:: |
public | function | ||
PluginCollectionArrayAdapter:: |
public | function | ||
PluginCollectionArrayAdapter:: |
public | function | ||
PluginCollectionArrayAdapter:: |
public | function | ||
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. | |
TagPluginCollection:: |
public static | function | Create a plugin collection directly from an array of tag plugins. | |
TagPluginCollection:: |
public | function |
Gets the current configuration of all plugins in this collection. Overrides DefaultLazyPluginCollection:: |
|
TagPluginCollection:: |
public | function | Generate a list of configured tags for display. | |
TagPluginCollection:: |
public | function | Generate a table of available tags, with samples. | |
TagPluginCollection:: |
public | function |
Determines if a plugin instance exists. Overrides LazyPluginCollection:: |
|
TagPluginCollection:: |
public | function |
Sets the configuration for all plugins in this collection. Overrides DefaultLazyPluginCollection:: |
|
TagPluginCollection:: |
public | function |
Provides uasort() callback to sort plugins. Overrides DefaultLazyPluginCollection:: |
|
TagPluginCollection:: |
public | function |
Constructs a new DefaultLazyPluginCollection object. Overrides DefaultLazyPluginCollection:: |