You are here

public function SectionStorageManager::findByContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php \Drupal\layout_builder\SectionStorage\SectionStorageManager::findByContext()

Finds the section storage to load based on available contexts.

Parameters

\Drupal\Component\Plugin\Context\ContextInterface[] $contexts: The contexts which should be used to determine which storage to return.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability: Refinable cacheability object, which will be populated based on the cacheability of each section storage candidate. After calling this method this parameter will reflect the cacheability information used to determine the correct section storage. This must be associated with any output that uses the result of this method.

Return value

\Drupal\layout_builder\SectionStorageInterface|null The section storage if one matched all contexts, or NULL otherwise.

Overrides SectionStorageManagerInterface::findByContext

See also

\Drupal\Core\Cache\RefinableCacheableDependencyInterface

File

core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php, line 87

Class

SectionStorageManager
Provides the Section Storage type plugin manager.

Namespace

Drupal\layout_builder\SectionStorage

Code

public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability) {
  $storage_types = array_keys($this->contextHandler
    ->filterPluginDefinitionsByContexts($contexts, $this
    ->getDefinitions()));

  // Add the manager as a cacheable dependency in order to vary by changes to
  // the plugin definitions.
  $cacheability
    ->addCacheableDependency($this);
  foreach ($storage_types as $type) {
    $plugin = $this
      ->load($type, $contexts);
    if ($plugin && $plugin
      ->isApplicable($cacheability)) {
      return $plugin;
    }
  }
  return NULL;
}