The document discusses Drupal 8's plugin system and how to create custom plugins. It explains that all plugins should be placed in a src/Plugin/{plugin_name} directory and defined using annotations. Derivative plugins allow extending core plugins, and are defined by specifying a deriver class. The plugin manager handles plugin discovery, creation, and provides available plugins. Plugins can be discovered via annotations, YAML, static definitions, or hooks, and discovery classes can be decorated to combine methods.
22. HOWTOUSE
Get a list of available plugins:
$type = Drupal::service('plugin.manager.sandwich');
/**
* @var DrupalmymoduleSandwichInterface[] $plugins
*/
$plugins = $type>getDefinitions();
In this case you will receive only objects that implement
your interface
24. ANNOTATIONDISCOVERY
You already saw it
/**
* @Block(
* id = "system_menu_block",
* admin_label = @Translation("Menu"),
* )
*/
This is much better then ctools $plugin = array();
Using in plugin manager
new AnnotatedClassDiscovery(
"Plugin/Sandwich", // Subdirectory
$namespaces, // Service "container.namespaces"
"DrupalCoreBlockAnnotationBlock" // Annotation @Block
);
25. YAMLDISCOVERY
Usefull for theme plugins, like breakpoints or layouts
core/themes/bartik/bartik.breakpoints.yml
bartik.wide:
label: wide
mediaQuery: 'all and (minwidth: 851px)'
multipliers:
1x
DrupalbreakpointBreakpointManager::$default
contains all possible keys
$directories = $moduleHandler>getModuleDirectories() +
$themeHandler>getThemeDirectories();
new YamlDiscovery('breakpoints', $directories);
26. STATICDISCOVERY
Useful when you need to add 3rd-party class as plugin
class SandwichPluginManager extends DefaultPluginManager {
protected function getDiscovery() {
$this>discovery = new StaticDiscovery();
$this>discovery>setDefinition('cheese_sandwich', [
'label' => new TranslatableMarkup('Cheese Sandwich'),
'class' => 'SomeVendorLibraryCheeseSandwich',
]);
return $this>discovery;
}
}
28. DISCOVERYDECORATORS
Combine them all together with decorators
$d = new AnnotatedClassDiscovery("Plugin/sandwich", $nmspaces);
$d = new ContainerDerivativeDiscoveryDecorator($d);
$d = new YamlDiscoveryDecorator($d, 'sandwich', $directories);
$d = new InfoHookDecorator($d, 'sandwich_info');
$d = new StaticDiscoveryDecorator($d, "callback");