Drupal Plugin discovery
Plugin discovery is the process by which Drupal finds plugins of a given type. A discovery method must be set for every plugin type (explained in the plugin manager documentation).
The discovery component of plugins implements a DiscoveryInterface that defines the methods any discovery class must have.
/**
* @file
* Contains \Drupal\Component\Plugin\Discovery\DiscoveryInterface.
*/
namespace Drupal\Component\Plugin\Discovery;
/**
* An interface defining the minimum requirements of building a plugin
* discovery component.
*
* @ingroup plugin_api
*/
interface DiscoveryInterface {
/**
* Gets a specific plugin definition.
*
* @param string $plugin_id
* A plugin id.
* @param bool $exception_on_invalid
* (optional) If TRUE, an invalid plugin ID will throw an exception.
*
* @return mixed
* A plugin definition, or NULL if the plugin ID is invalid and
* $exception_on_invalid is FALSE.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* Thrown if $plugin_id is invalid and $exception_on_invalid is TRUE.
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE);
/**
* Gets the definition of all plugins for this type.
*
* @return mixed[]
* An array of plugin definitions (empty array if no definitions were
* found). Keys are plugin IDs.
*/
public function getDefinitions();
/**
* Indicates if a specific plugin definition exists.
*
* @param string $plugin_id
* A plugin ID.
*
* @return bool
* TRUE if the definition exists, FALSE otherwise.
*/
public function hasDefinition($plugin_id);
}
These are the different core discovery types.
- StaticDiscovery
StaticDiscovery allows for direct registration of plugins within the discovery class itself. A protected property (
$definitions) in the class holds all plugin definitions that are registered with it through the public methodsetDefinition(). Any plugin defined through this method can then be invoked as outlined in the plugin manager documentation. - HookDiscovery
The HookDiscovery class allows Drupal's
hook_component_info()/hook_component_info_alter()pattern to be used for plugin discovery. With this discovery, the plugin manager will invoke info hooks to retrieve a list of available plugins. - AnnotatedClassDiscovery
New plugin implementations should use AttributeClassDiscovery.
The AnnotatedClassDiscovery class uses name of the annotations that contains the plugin definition, e.g.,
@Plugin,@EntityType, in plugin docblocks to discover plugins, minimizing memory usage during the discovery phase. The AnnotatedClassDiscovery class takes an argument in its constructor,$subdir, which specifies the sub-directory/sub-namespace for this plugin type. The AnnotatedClassDiscovery class scans PSR-4 classes inside those sub-directories of Plugin folders to find plugins (see a "Annotations-based plugins"). - YamlDiscovery
YamlDiscovery allows plugins to be defined in yaml files. Drupal core uses this for local tasks and local actions.
- AttributeClassDiscovery
The AttributeClassDiscovery class allows plugins to be defined using PHP attributes. See Attribute-based plugins.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.