Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / lib / Drupal / Core / Layout / LayoutPluginManager.php
1 <?php
2
3 namespace Drupal\Core\Layout;
4
5 use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
6 use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Extension\ThemeHandlerInterface;
10 use Drupal\Core\Plugin\DefaultPluginManager;
11 use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
12 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
13 use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
14 use Drupal\Core\Layout\Annotation\Layout;
15 use Drupal\Core\Plugin\FilteredPluginManagerTrait;
16
17 /**
18  * Provides a plugin manager for layouts.
19  */
20 class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginManagerInterface {
21
22   use FilteredPluginManagerTrait;
23
24   /**
25    * The theme handler.
26    *
27    * @var \Drupal\Core\Extension\ThemeHandlerInterface
28    */
29   protected $themeHandler;
30
31   /**
32    * LayoutPluginManager constructor.
33    *
34    * @param \Traversable $namespaces
35    *   An object that implements \Traversable which contains the root paths
36    *   keyed by the corresponding namespace to look for plugin implementations.
37    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
38    *   Cache backend instance to use.
39    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
40    *   The module handler to invoke the alter hook with.
41    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
42    *   The theme handler to invoke the alter hook with.
43    */
44   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
45     parent::__construct('Plugin/Layout', $namespaces, $module_handler, LayoutInterface::class, Layout::class);
46     $this->themeHandler = $theme_handler;
47
48     $type = $this->getType();
49     $this->setCacheBackend($cache_backend, $type);
50     $this->alterInfo($type);
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function getType() {
57     return 'layout';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function providerExists($provider) {
64     return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   protected function getDiscovery() {
71     if (!$this->discovery) {
72       $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
73       $discovery = new YamlDiscoveryDecorator($discovery, 'layouts', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories());
74       $discovery = new AnnotationBridgeDecorator($discovery, $this->pluginDefinitionAnnotationName);
75       $discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
76       $this->discovery = $discovery;
77     }
78     return $this->discovery;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function processDefinition(&$definition, $plugin_id) {
85     parent::processDefinition($definition, $plugin_id);
86
87     if (!$definition instanceof LayoutDefinition) {
88       throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" layout definition must extend %s', $plugin_id, LayoutDefinition::class));
89     }
90
91     // Add the module or theme path to the 'path'.
92     $provider = $definition->getProvider();
93     if ($this->moduleHandler->moduleExists($provider)) {
94       $base_path = $this->moduleHandler->getModule($provider)->getPath();
95     }
96     elseif ($this->themeHandler->themeExists($provider)) {
97       $base_path = $this->themeHandler->getTheme($provider)->getPath();
98     }
99     else {
100       $base_path = '';
101     }
102
103     $path = $definition->getPath();
104     $path = !empty($path) ? $base_path . '/' . $path : $base_path;
105     $definition->setPath($path);
106
107     // Add the base path to the icon path.
108     if ($icon_path = $definition->getIconPath()) {
109       $definition->setIconPath($path . '/' . $icon_path);
110     }
111
112     // Add a dependency on the provider of the library.
113     if ($library = $definition->getLibrary()) {
114       $config_dependencies = $definition->getConfigDependencies();
115       list($library_provider) = explode('/', $library, 2);
116       if ($this->moduleHandler->moduleExists($library_provider)) {
117         $config_dependencies['module'][] = $library_provider;
118       }
119       elseif ($this->themeHandler->themeExists($library_provider)) {
120         $config_dependencies['theme'][] = $library_provider;
121       }
122       $definition->setConfigDependencies($config_dependencies);
123     }
124
125     // If 'template' is set, then we'll derive 'template_path' and 'theme_hook'.
126     $template = $definition->getTemplate();
127     if (!empty($template)) {
128       $template_parts = explode('/', $template);
129
130       $template = array_pop($template_parts);
131       $template_path = $path;
132       if (count($template_parts) > 0) {
133         $template_path .= '/' . implode('/', $template_parts);
134       }
135       $definition->setTemplate($template);
136       $definition->setThemeHook(strtr($template, '-', '_'));
137       $definition->setTemplatePath($template_path);
138     }
139
140     if (!$definition->getDefaultRegion()) {
141       $definition->setDefaultRegion(key($definition->getRegions()));
142     }
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getThemeImplementations() {
149     $hooks = [];
150     $hooks['layout'] = [
151       'render element' => 'content',
152     ];
153     /** @var \Drupal\Core\Layout\LayoutDefinition[] $definitions */
154     $definitions = $this->getDefinitions();
155     foreach ($definitions as $definition) {
156       if ($template = $definition->getTemplate()) {
157         $hooks[$definition->getThemeHook()] = [
158           'render element' => 'content',
159           'base hook' => 'layout',
160           'template' => $template,
161           'path' => $definition->getTemplatePath(),
162         ];
163       }
164     }
165     return $hooks;
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function getCategories() {
172     // Fetch all categories from definitions and remove duplicates.
173     $categories = array_unique(array_values(array_map(function (LayoutDefinition $definition) {
174       return $definition->getCategory();
175     }, $this->getDefinitions())));
176     natcasesort($categories);
177     return $categories;
178   }
179
180   /**
181    * {@inheritdoc}
182    *
183    * @return \Drupal\Core\Layout\LayoutDefinition[]
184    */
185   public function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') {
186     // Sort the plugins first by category, then by label.
187     $definitions = isset($definitions) ? $definitions : $this->getDefinitions();
188     // Suppress errors because PHPUnit will indirectly modify the contents,
189     // triggering https://bugs.php.net/bug.php?id=50688.
190     @uasort($definitions, function (LayoutDefinition $a, LayoutDefinition $b) {
191       if ($a->getCategory() != $b->getCategory()) {
192         return strnatcasecmp($a->getCategory(), $b->getCategory());
193       }
194       return strnatcasecmp($a->getLabel(), $b->getLabel());
195     });
196     return $definitions;
197   }
198
199   /**
200    * {@inheritdoc}
201    *
202    * @return \Drupal\Core\Layout\LayoutDefinition[][]
203    */
204   public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
205     $definitions = $this->getSortedDefinitions(isset($definitions) ? $definitions : $this->getDefinitions(), $label_key);
206     $grouped_definitions = [];
207     foreach ($definitions as $id => $definition) {
208       $grouped_definitions[(string) $definition->getCategory()][$id] = $definition;
209     }
210     return $grouped_definitions;
211   }
212
213   /**
214    * {@inheritdoc}
215    */
216   public function getLayoutOptions() {
217     $layout_options = [];
218     foreach ($this->getGroupedDefinitions() as $category => $layout_definitions) {
219       foreach ($layout_definitions as $name => $layout_definition) {
220         $layout_options[$category][$name] = $layout_definition->getLabel();
221       }
222     }
223     return $layout_options;
224   }
225
226 }