You are here

class TagPluginCollection in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/TagPluginCollection.php \Drupal\xbbcode\TagPluginCollection

A collection of tag plugins.

@property \Drupal\xbbcode\TagPluginManager manager

Hierarchy

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\xbbcode
View 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

Namesort descending Modifiers Type Description Overrides
DefaultLazyPluginCollection::$configurations protected property The initial configuration for each plugin in the collection.
DefaultLazyPluginCollection::$manager protected property The manager used to instantiate the plugins.
DefaultLazyPluginCollection::$originalOrder protected property The original order of the instances.
DefaultLazyPluginCollection::$pluginKey protected property The key within the plugin configuration that contains the plugin ID. 3
DefaultLazyPluginCollection::addInstanceId public function Adds an instance ID to the available instance IDs. Overrides LazyPluginCollection::addInstanceId
DefaultLazyPluginCollection::initializePlugin protected function Initializes and stores a plugin. Overrides LazyPluginCollection::initializePlugin 2
DefaultLazyPluginCollection::removeInstanceId public function Removes an instance ID. Overrides LazyPluginCollection::removeInstanceId
DefaultLazyPluginCollection::setInstanceConfiguration public function Updates the configuration for a plugin instance.
DefaultLazyPluginCollection::sort public function Sorts all plugin instances in this collection. 1
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
LazyPluginCollection::$instanceIds protected property Stores the IDs of all potential plugin instances.
LazyPluginCollection::$pluginInstances protected property Stores all instantiated plugins.
LazyPluginCollection::clear public function Clears all instantiated plugins.
LazyPluginCollection::count public function
LazyPluginCollection::get public function Gets a plugin instance, initializing it if necessary. 4
LazyPluginCollection::getInstanceIds public function Gets all instance IDs.
LazyPluginCollection::getIterator public function
LazyPluginCollection::remove public function Removes an initialized plugin.
LazyPluginCollection::set public function Stores an initialized plugin.
PluginCollectionArrayAdapter::offsetExists public function
PluginCollectionArrayAdapter::offsetGet public function
PluginCollectionArrayAdapter::offsetSet public function
PluginCollectionArrayAdapter::offsetUnset public function
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.
TagPluginCollection::createFromTags public static function Create a plugin collection directly from an array of tag plugins.
TagPluginCollection::getConfiguration public function Gets the current configuration of all plugins in this collection. Overrides DefaultLazyPluginCollection::getConfiguration
TagPluginCollection::getSummary public function Generate a list of configured tags for display.
TagPluginCollection::getTable public function Generate a table of available tags, with samples.
TagPluginCollection::has public function Determines if a plugin instance exists. Overrides LazyPluginCollection::has
TagPluginCollection::setConfiguration public function Sets the configuration for all plugins in this collection. Overrides DefaultLazyPluginCollection::setConfiguration
TagPluginCollection::sortHelper public function Provides uasort() callback to sort plugins. Overrides DefaultLazyPluginCollection::sortHelper
TagPluginCollection::__construct public function Constructs a new DefaultLazyPluginCollection object. Overrides DefaultLazyPluginCollection::__construct