You are here

class EntityTagPlugin in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/XBBCode/EntityTagPlugin.php \Drupal\xbbcode\Plugin\XBBCode\EntityTagPlugin

A tag plugin based on a custom tag entity.

Plugin annotation


@XBBCodeTag(
 id = "xbbcode_tag",
 label = "Custom tag",
 admin_label = @Translation("Custom tag"),
 category = @Translation("Custom"),
 deriver = "Drupal\xbbcode\Plugin\Derivative\TagPluginDeriver"
)

Hierarchy

Expanded class hierarchy of EntityTagPlugin

2 files declare their use of EntityTagPlugin
TagForm.php in src/Form/TagForm.php
TagFormBase.php in src/Form/TagFormBase.php

File

src/Plugin/XBBCode/EntityTagPlugin.php, line 26

Namespace

Drupal\xbbcode\Plugin\XBBCode
View source
class EntityTagPlugin extends TemplateTagPlugin implements ContainerFactoryPluginInterface {

  /**
   * The prefix that precedes an inline template.
   *
   * @var string
   */
  public const TEMPLATE_PREFIX = '{# inline_template_start #}';

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * The custom tag entity this plugin is derived from.
   *
   * (Not serialized for performance reasons.)
   *
   * @var \Drupal\xbbcode\Entity\TagInterface
   */
  private $entity;

  /**
   * Constructs a new custom tag plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Twig\Environment $twig
   *   The twig template loader.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The tag storage.
   */
  public function __construct(array $configuration, string $plugin_id, $plugin_definition, TwigEnvironment $twig, EntityStorageInterface $storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $twig);
    $this->storage = $storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('twig'), $container
      ->get('entity_type.manager')
      ->getStorage('xbbcode_tag'));
  }

  /**
   * {@inheritdoc}
   */
  public function getTemplate() : TwigTemplateWrapper {

    // Lazily prepare the template, if it does not exist yet.
    if ($this->template === NULL) {
      $entity = $this
        ->getEntity();
      $code = $entity
        ->getTemplateCode();
      $file = $entity
        ->getTemplateFile();
      $this->template = $file && !$code ? $file : self::TEMPLATE_PREFIX . $code;
    }

    // Delegate template-loading to the parent.
    return parent::getTemplate();
  }

  /**
   * {@inheritdoc}
   */
  public function doProcess(TagElementInterface $tag) : TagProcessResult {
    $result = parent::doProcess($tag);
    $result
      ->addCacheableDependency($this
      ->getEntity());
    $result
      ->addAttachments($this
      ->getEntity()
      ->getAttachments());
    return $result;
  }

  /**
   * Loads the custom tag entity of the plugin.
   *
   * @return \Drupal\xbbcode\Entity\TagInterface
   *   The custom tag entity.
   */
  protected function getEntity() : TagInterface {
    if (!$this->entity) {
      $id = $this
        ->getDerivativeId();
      $this->entity = $this->storage
        ->load($id);
    }
    return $this->entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityTagPlugin::$entity private property The custom tag entity this plugin is derived from.
EntityTagPlugin::$storage protected property The entity storage.
EntityTagPlugin::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntityTagPlugin::doProcess public function Create the actual output. Overrides TemplateTagPlugin::doProcess
EntityTagPlugin::getEntity protected function Loads the custom tag entity of the plugin.
EntityTagPlugin::getTemplate public function Get the tag template. Overrides TemplateTagPlugin::getTemplate
EntityTagPlugin::TEMPLATE_PREFIX public constant The prefix that precedes an inline template.
EntityTagPlugin::__construct public function Constructs a new custom tag plugin. Overrides TemplateTagPlugin::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
TagPluginBase::$name protected property The configurable tag name.
TagPluginBase::$sample protected property The sample code of this tag.
TagPluginBase::$settings protected property The settings for this tag plugin.
TagPluginBase::$status protected property A Boolean indicating whether this tag is enabled.
TagPluginBase::defaultConfiguration public function Get default plugin configuration from definition.
TagPluginBase::getConfiguration public function Get the plugin configuration.
TagPluginBase::getDefaultName public function Returns the default tag name. Overrides TagPluginInterface::getDefaultName
TagPluginBase::getDefaultSample public function Return the unprocessed sample code. Overrides TagPluginInterface::getDefaultSample 3
TagPluginBase::getDescription public function Returns the administrative description for this tag plugin. Overrides TagPluginInterface::getDescription
TagPluginBase::getName public function Returns the configured name. Overrides TagPluginInterface::getName
TagPluginBase::getSample public function Return a sample tag for the filter tips. Overrides TagPluginInterface::getSample
TagPluginBase::label public function Returns the administrative label for this tag plugin. Overrides TagPluginInterface::label
TagPluginBase::prepare public function Transform an elements' content, to armor against other filters. Overrides TagPluginInterface::prepare 2
TagPluginBase::process public function Generate output from a tag element. Overrides TagPluginInterface::process
TagPluginBase::setConfiguration public function Set the plugin configuration after instancing.
TagPluginBase::status public function Returns the status of this tag plugin. Overrides TagPluginInterface::status
TemplateTagPlugin::$template protected property The serializable identifier of the template.
TemplateTagPlugin::$templateWrapper private property Ephemeral reference to the template.
TemplateTagPlugin::$twig protected property The twig environment.