3 namespace Drupal\Core\Layout;
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;
18 * Provides a plugin manager for layouts.
20 class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginManagerInterface {
22 use FilteredPluginManagerTrait;
27 * @var \Drupal\Core\Extension\ThemeHandlerInterface
29 protected $themeHandler;
32 * LayoutPluginManager constructor.
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.
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;
48 $type = $this->getType();
49 $this->setCacheBackend($cache_backend, $type);
50 $this->alterInfo($type);
56 protected function getType() {
63 protected function providerExists($provider) {
64 return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider);
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;
78 return $this->discovery;
84 public function processDefinition(&$definition, $plugin_id) {
85 parent::processDefinition($definition, $plugin_id);
87 if (!$definition instanceof LayoutDefinition) {
88 throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" layout definition must extend %s', $plugin_id, LayoutDefinition::class));
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();
96 elseif ($this->themeHandler->themeExists($provider)) {
97 $base_path = $this->themeHandler->getTheme($provider)->getPath();
103 $path = $definition->getPath();
104 $path = !empty($path) ? $base_path . '/' . $path : $base_path;
105 $definition->setPath($path);
107 // Add the base path to the icon path.
108 if ($icon_path = $definition->getIconPath()) {
109 $definition->setIconPath($path . '/' . $icon_path);
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;
119 elseif ($this->themeHandler->themeExists($library_provider)) {
120 $config_dependencies['theme'][] = $library_provider;
122 $definition->setConfigDependencies($config_dependencies);
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);
130 $template = array_pop($template_parts);
131 $template_path = $path;
132 if (count($template_parts) > 0) {
133 $template_path .= '/' . implode('/', $template_parts);
135 $definition->setTemplate($template);
136 $definition->setThemeHook(strtr($template, '-', '_'));
137 $definition->setTemplatePath($template_path);
140 if (!$definition->getDefaultRegion()) {
141 $definition->setDefaultRegion(key($definition->getRegions()));
148 public function getThemeImplementations() {
151 'render element' => 'content',
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(),
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);
183 * @return \Drupal\Core\Layout\LayoutDefinition[]
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());
194 return strnatcasecmp($a->getLabel(), $b->getLabel());
202 * @return \Drupal\Core\Layout\LayoutDefinition[][]
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;
210 return $grouped_definitions;
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();
223 return $layout_options;