PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php

http://github.com/drupal/drupal
PHP | 132 lines | 66 code | 17 blank | 49 comment | 3 complexity | 4c079ef507bb48f820660f99f814ef32 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\layout_builder\SectionStorage;
  3. use Drupal\Component\Plugin\Exception\ContextException;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Plugin\Context\ContextHandlerInterface;
  8. use Drupal\Core\Plugin\DefaultPluginManager;
  9. use Drupal\layout_builder\Annotation\SectionStorage;
  10. use Drupal\layout_builder\SectionStorageInterface;
  11. /**
  12. * Provides the Section Storage type plugin manager.
  13. *
  14. * Note that while this class extends \Drupal\Core\Plugin\DefaultPluginManager
  15. * and includes many additional public methods, only some of them are available
  16. * via \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface.
  17. * While internally depending on the parent class is necessary, external code
  18. * should only use the methods available on that interface.
  19. */
  20. class SectionStorageManager extends DefaultPluginManager implements SectionStorageManagerInterface {
  21. /**
  22. * The context handler.
  23. *
  24. * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
  25. */
  26. protected $contextHandler;
  27. /**
  28. * Constructs a new SectionStorageManager object.
  29. *
  30. * @param \Traversable $namespaces
  31. * An object that implements \Traversable which contains the root paths
  32. * keyed by the corresponding namespace to look for plugin implementations.
  33. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  34. * Cache backend instance to use.
  35. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  36. * The module handler to invoke the alter hook with.
  37. * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
  38. * The context handler.
  39. */
  40. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ContextHandlerInterface $context_handler = NULL) {
  41. parent::__construct('Plugin/SectionStorage', $namespaces, $module_handler, SectionStorageInterface::class, SectionStorage::class);
  42. if (!$context_handler) {
  43. @trigger_error('The context.handler service must be passed to \Drupal\layout_builder\SectionStorage\SectionStorageManager::__construct(); it was added in Drupal 8.7.0 and will be required before Drupal 9.0.0.', E_USER_DEPRECATED);
  44. $context_handler = \Drupal::service('context.handler');
  45. }
  46. $this->contextHandler = $context_handler;
  47. $this->alterInfo('layout_builder_section_storage');
  48. $this->setCacheBackend($cache_backend, 'layout_builder_section_storage_plugins');
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function findDefinitions() {
  54. $definitions = parent::findDefinitions();
  55. // Sort the definitions by their weight while preserving the original order
  56. // for those with matching weights.
  57. $weights = array_map(function (SectionStorageDefinition $definition) {
  58. return $definition->getWeight();
  59. }, $definitions);
  60. $ids = array_keys($definitions);
  61. array_multisort($weights, $ids, $definitions);
  62. return $definitions;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function load($type, array $contexts = []) {
  68. $plugin = $this->loadEmpty($type);
  69. try {
  70. $this->contextHandler->applyContextMapping($plugin, $contexts);
  71. }
  72. catch (ContextException $e) {
  73. return NULL;
  74. }
  75. return $plugin;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability) {
  81. $storage_types = array_keys($this->contextHandler->filterPluginDefinitionsByContexts($contexts, $this->getDefinitions()));
  82. // Add the manager as a cacheable dependency in order to vary by changes to
  83. // the plugin definitions.
  84. $cacheability->addCacheableDependency($this);
  85. foreach ($storage_types as $type) {
  86. $plugin = $this->load($type, $contexts);
  87. if ($plugin && $plugin->isApplicable($cacheability)) {
  88. return $plugin;
  89. }
  90. }
  91. return NULL;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function loadEmpty($type) {
  97. return $this->createInstance($type);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function loadFromStorageId($type, $id) {
  103. @trigger_error('\Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::loadFromStorageId() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::load() should be used instead. See https://www.drupal.org/node/3012353.', E_USER_DEPRECATED);
  104. $contexts = $this->loadEmpty($type)->deriveContextsFromRoute($id, [], '', []);
  105. return $this->load($type, $contexts);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function loadFromRoute($type, $value, $definition, $name, array $defaults) {
  111. @trigger_error('\Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::loadFromRoute() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. \Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute() and \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::load() should be used instead. See https://www.drupal.org/node/3012353.', E_USER_DEPRECATED);
  112. $contexts = $this->loadEmpty($type)->deriveContextsFromRoute($value, $definition, $name, $defaults);
  113. return $this->load($type, $contexts);
  114. }
  115. }