3 namespace Drupal\Core\Plugin;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
7 use Drupal\Core\Theme\ThemeManagerInterface;
10 * Provides a trait for plugin managers that allow filtering plugin definitions.
12 trait FilteredPluginManagerTrait {
14 use ContextAwarePluginManagerTrait;
17 * Implements \Drupal\Core\Plugin\FilteredPluginManagerInterface::getFilteredDefinitions().
19 public function getFilteredDefinitions($consumer, $contexts = NULL, array $extra = []) {
20 if (!is_null($contexts)) {
21 $definitions = $this->getDefinitionsForContexts($contexts);
24 $definitions = $this->getDefinitions();
27 $type = $this->getType();
29 $hooks[] = "plugin_filter_{$type}";
30 $hooks[] = "plugin_filter_{$type}__{$consumer}";
31 $this->moduleHandler()->alter($hooks, $definitions, $extra, $consumer);
32 $this->themeManager()->alter($hooks, $definitions, $extra, $consumer);
37 * A string identifying the plugin type.
39 * This string should be unique and generally will correspond to the string
40 * used by the discovery, e.g. the annotation class or the YAML file name.
43 * A string identifying the plugin type.
45 abstract protected function getType();
48 * Wraps the module handler.
50 * @return \Drupal\Core\Extension\ModuleHandlerInterface
53 protected function moduleHandler() {
54 if (property_exists($this, 'moduleHandler') && $this->moduleHandler instanceof ModuleHandlerInterface) {
55 return $this->moduleHandler;
58 return \Drupal::service('module_handler');
62 * Wraps the theme manager.
64 * @return \Drupal\Core\Theme\ThemeManagerInterface
67 protected function themeManager() {
68 if (property_exists($this, 'themeManager') && $this->themeManager instanceof ThemeManagerInterface) {
69 return $this->themeManager;
72 return \Drupal::service('theme.manager');