3 namespace Drupal\Core\Layout;
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;
17 * Provides a plugin manager for layouts.
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.
24 class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginManagerInterface {
29 * @var \Drupal\Core\Extension\ThemeHandlerInterface
31 protected $themeHandler;
34 * LayoutPluginManager constructor.
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.
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;
50 $this->setCacheBackend($cache_backend, 'layout');
51 $this->alterInfo('layout');
57 protected function providerExists($provider) {
58 return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
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;
72 return $this->discovery;
78 public function processDefinition(&$definition, $plugin_id) {
79 parent::processDefinition($definition, $plugin_id);
81 if (!$definition instanceof LayoutDefinition) {
82 throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" layout definition must extend %s', $plugin_id, LayoutDefinition::class));
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();
90 elseif ($this->themeHandler->themeExists($provider)) {
91 $base_path = $this->themeHandler->getTheme($provider)->getPath();
97 $path = $definition->getPath();
98 $path = !empty($path) ? $base_path . '/' . $path : $base_path;
99 $definition->setPath($path);
101 // Add the base path to the icon path.
102 if ($icon_path = $definition->getIconPath()) {
103 $definition->setIconPath($path . '/' . $icon_path);
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;
113 elseif ($this->themeHandler->themeExists($library_provider)) {
114 $config_dependencies['theme'][] = $library_provider;
116 $definition->setConfigDependencies($config_dependencies);
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);
124 $template = array_pop($template_parts);
125 $template_path = $path;
126 if (count($template_parts) > 0) {
127 $template_path .= '/' . implode('/', $template_parts);
129 $definition->setTemplate($template);
130 $definition->setThemeHook(strtr($template, '-', '_'));
131 $definition->setTemplatePath($template_path);
134 if (!$definition->getDefaultRegion()) {
135 $definition->setDefaultRegion(key($definition->getRegions()));
142 public function getThemeImplementations() {
145 'render element' => 'content',
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(),
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);
177 * @return \Drupal\Core\Layout\LayoutDefinition[]
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());
188 return strnatcasecmp($a->getLabel(), $b->getLabel());
196 * @return \Drupal\Core\Layout\LayoutDefinition[][]
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;
204 return $grouped_definitions;
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();
217 return $layout_options;