"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php" (18 Nov 2020, 5386 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SectionStorageManager.php" see the Fossies "Dox" file reference documentation.

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