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