trait DiscoveryTrait

Same name in other branches
  1. 9 core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php \Drupal\Component\Plugin\Discovery\DiscoveryTrait
  2. 10 core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php \Drupal\Component\Plugin\Discovery\DiscoveryTrait
  3. 11.x core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php \Drupal\Component\Plugin\Discovery\DiscoveryTrait

Hierarchy

See also

Drupal\Component\Plugin\Discovery\DiscoveryInterface

10 files declare their use of DiscoveryTrait
AnnotatedClassDiscovery.php in core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
AnnotationBridgeDecorator.php in core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotationBridgeDecorator.php
ConfigSchemaDiscovery.php in core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php
HelpTopicDiscovery.php in core/modules/help_topics/src/HelpTopicDiscovery.php
HookDiscovery.php in core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php

... See full list

File

core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php, line 10

Namespace

Drupal\Component\Plugin\Discovery
View source
trait DiscoveryTrait {
    
    /**
     * {@inheritdoc}
     */
    public abstract function getDefinitions();
    
    /**
     * {@inheritdoc}
     */
    public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
        $definitions = $this->getDefinitions();
        return $this->doGetDefinition($definitions, $plugin_id, $exception_on_invalid);
    }
    
    /**
     * Gets a specific plugin definition.
     *
     * @param array $definitions
     *   An array of the available plugin definitions.
     * @param string $plugin_id
     *   A plugin id.
     * @param bool $exception_on_invalid
     *   If TRUE, an invalid plugin ID will cause an exception to be thrown; if
     *   FALSE, NULL will be returned.
     *
     * @return array|null
     *   A plugin definition, or NULL if the plugin ID is invalid and
     *   $exception_on_invalid is TRUE.
     *
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     *   Thrown if $plugin_id is invalid and $exception_on_invalid is TRUE.
     */
    protected function doGetDefinition(array $definitions, $plugin_id, $exception_on_invalid) {
        // Avoid using a ternary that would create a copy of the array.
        if (isset($definitions[$plugin_id])) {
            return $definitions[$plugin_id];
        }
        elseif (!$exception_on_invalid) {
            return NULL;
        }
        $valid_ids = implode(', ', array_keys($definitions));
        throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist. Valid plugin IDs for %s are: %s', $plugin_id, static::class, $valid_ids));
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasDefinition($plugin_id) {
        return (bool) $this->getDefinition($plugin_id, FALSE);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::getDefinition public function 3
DiscoveryTrait::getDefinitions abstract public function 13
DiscoveryTrait::hasDefinition public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.