PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Layout/LayoutPluginManager.php

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