Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / FilteredPluginManagerTrait.php
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
7 use Drupal\Core\Theme\ThemeManagerInterface;
8
9 /**
10  * Provides a trait for plugin managers that allow filtering plugin definitions.
11  */
12 trait FilteredPluginManagerTrait {
13
14   use ContextAwarePluginManagerTrait;
15
16   /**
17    * Implements \Drupal\Core\Plugin\FilteredPluginManagerInterface::getFilteredDefinitions().
18    */
19   public function getFilteredDefinitions($consumer, $contexts = NULL, array $extra = []) {
20     if (!is_null($contexts)) {
21       $definitions = $this->getDefinitionsForContexts($contexts);
22     }
23     else {
24       $definitions = $this->getDefinitions();
25     }
26
27     $type = $this->getType();
28     $hooks = [];
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);
33     return $definitions;
34   }
35
36   /**
37    * A string identifying the plugin type.
38    *
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.
41    *
42    * @return string
43    *   A string identifying the plugin type.
44    */
45   abstract protected function getType();
46
47   /**
48    * Wraps the module handler.
49    *
50    * @return \Drupal\Core\Extension\ModuleHandlerInterface
51    *   The module handler.
52    */
53   protected function moduleHandler() {
54     if (property_exists($this, 'moduleHandler') && $this->moduleHandler instanceof ModuleHandlerInterface) {
55       return $this->moduleHandler;
56     }
57
58     return \Drupal::service('module_handler');
59   }
60
61   /**
62    * Wraps the theme manager.
63    *
64    * @return \Drupal\Core\Theme\ThemeManagerInterface
65    *   The theme manager.
66    */
67   protected function themeManager() {
68     if (property_exists($this, 'themeManager') && $this->themeManager instanceof ThemeManagerInterface) {
69       return $this->themeManager;
70     }
71
72     return \Drupal::service('theme.manager');
73   }
74
75 }