You are here

class Plugin in Drupal 10

Same name in this branch
  1. 10 composer/Plugin/Scaffold/Plugin.php \Drupal\Composer\Plugin\Scaffold\Plugin
  2. 10 core/lib/Drupal/Component/Annotation/Plugin.php \Drupal\Component\Annotation\Plugin
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Annotation/Plugin.php \Drupal\Component\Annotation\Plugin
  2. 9 core/lib/Drupal/Component/Annotation/Plugin.php \Drupal\Component\Annotation\Plugin

Defines a Plugin annotation object.

Annotations in plugin classes can use this class in order to pass various metadata about the plugin through the parser to DiscoveryInterface::getDefinitions() calls. This allows the metadata of a class to be located with the class itself, rather than in module-based info hooks.

Hierarchy

Expanded class hierarchy of Plugin

Related topics

43 files declare their use of Plugin
Action.php in core/lib/Drupal/Core/Annotation/Action.php
AnnotatedClassDiscoveryTest.php in core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php
AnnotationBridgeDecoratorTest.php in core/tests/Drupal/Tests/Component/Annotation/Plugin/Discovery/AnnotationBridgeDecoratorTest.php
Archiver.php in core/lib/Drupal/Core/Archiver/Annotation/Archiver.php
Block.php in core/lib/Drupal/Core/Block/Annotation/Block.php

... See full list

7 string references to 'Plugin'
block.schema.yml in core/modules/block/config/schema/block.schema.yml
core/modules/block/config/schema/block.schema.yml
CKEditor5PluginManagerTest::providerTestInvalidPluginDefinitions in core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php
Data provider.
FieldDefinitionIntegrityTest::testFieldPluginDefinitionIntegrity in core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php
Tests the integrity of field plugin definitions.
LayoutPluginManagerTest::setUpFilesystem in core/tests/Drupal/Tests/Core/Layout/LayoutPluginManagerTest.php
Sets up the filesystem with YAML files and annotated plugins.
search.schema.yml in core/modules/search/config/schema/search.schema.yml
core/modules/search/config/schema/search.schema.yml

... See full list

11 classes are annotated with Plugin
Apple in core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/fruit/Apple.php
Plugin annotation @Plugin( id = "apple", label = "Apple", color = "green" )
Banana in core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/fruit/Banana.php
Plugin annotation @Plugin( id = "banana", label = "Banana", color = "yellow", uses = { "bread" = @Translation("Banana bread"), "loaf" = @PluralTranslation( singular = "@count loaf", plural = "@count loaves" ) } )
Broccoli in core/tests/Drupal/Tests/Component/Plugin/Fixtures/vegetable/Broccoli.php
Plugin annotation @Plugin( id = "broccoli", label = "Broccoli", color = "green" )
Cherry in core/modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/fruit/Cherry.php
Plugin annotation @Plugin( id = "cherry", label = "Cherry", color = "red" )
Corn in core/tests/Drupal/Tests/Component/Plugin/Fixtures/vegetable/Corn.php
Plugin annotation @Plugin( id = "corn", label = "Corn", color = "yellow" )

... See full list

File

core/lib/Drupal/Component/Annotation/Plugin.php, line 20

Namespace

Drupal\Component\Annotation
View source
class Plugin implements AnnotationInterface {

  /**
   * The plugin definition read from the class annotation.
   *
   * @var array
   */
  protected $definition;

  /**
   * Constructs a Plugin object.
   *
   * Builds up the plugin definition and invokes the get() method for any
   * classed annotations that were used.
   */
  public function __construct($values) {
    $reflection = new \ReflectionClass($this);

    // Only keep actual default values by ignoring NULL values.
    $defaults = array_filter($reflection
      ->getDefaultProperties(), function ($value) {
      return $value !== NULL;
    });
    $parsed_values = $this
      ->parse($values);
    $this->definition = NestedArray::mergeDeepArray([
      $defaults,
      $parsed_values,
    ], TRUE);
  }

  /**
   * Parses an annotation into its definition.
   *
   * @param array $values
   *   The annotation array.
   *
   * @return array
   *   The parsed annotation as a definition.
   */
  protected function parse(array $values) {
    $definitions = [];
    foreach ($values as $key => $value) {
      if ($value instanceof AnnotationInterface) {
        $definitions[$key] = $value
          ->get();
      }
      elseif (is_array($value)) {
        $definitions[$key] = $this
          ->parse($value);
      }
      else {
        $definitions[$key] = $value;
      }
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this->definition;
  }

  /**
   * {@inheritdoc}
   */
  public function getProvider() {
    return $this->definition['provider'] ?? FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setProvider($provider) {
    $this->definition['provider'] = $provider;
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->definition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getClass() {
    return $this->definition['class'];
  }

  /**
   * {@inheritdoc}
   */
  public function setClass($class) {
    $this->definition['class'] = $class;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Plugin::$definition protected property The plugin definition read from the class annotation. 1
Plugin::get public function Gets the value of an annotation. Overrides AnnotationInterface::get 6
Plugin::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass 1
Plugin::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId
Plugin::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider 1
Plugin::parse protected function Parses an annotation into its definition.
Plugin::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass 1
Plugin::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider
Plugin::__construct public function Constructs a Plugin object. 3