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.
<?php
/**
* @file
* Contains \Drupal\Component\Plugin\Discovery\DiscoveryInterface.
*/
namespace Drupal\Component\Plugin\Discovery;
/**
* Defines the plugin discovery interface.
*/
interface DiscoveryInterface {
/**
* Gets a specific plugin definition.
*
* @param string $plugin_id
* A plugin id.
*
* @return array
* A plugin definition.
*/
public function getPluginDefinition($plugin_id);
/**
* Gets the definition of all plugins for this type.
*
* @return array
* An array of configuration definitions.
*/
public function getPluginDefinitions();
}
?>
There are three different core discovery types.
- StaticDiscovery
StaticDiscovery allows for direct registration of plugins within the discovery class itself. A protected variable (
$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
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 two arguments in its constructor,$owner
and$type
. The$owner
is the module that owns the plugins type, and$type
is the plugin type. The AnnotatedClassDiscovery class scans PSR-0 classes inside Plugin folders to find plugins.