Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / Context / LayoutBuilderContextTrait.php
1 <?php
2
3 namespace Drupal\layout_builder\Context;
4
5 use Drupal\Core\Plugin\Context\ContextInterface;
6 use Drupal\layout_builder\SectionStorageInterface;
7
8 /**
9  * Provides a wrapper around getting contexts from a section storage object.
10  */
11 trait LayoutBuilderContextTrait {
12
13   /**
14    * The context repository.
15    *
16    * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
17    */
18   protected $contextRepository;
19
20   /**
21    * Gets the context repository service.
22    *
23    * @return \Drupal\Core\Plugin\Context\ContextRepositoryInterface
24    *   The context repository service.
25    */
26   protected function contextRepository() {
27     if (!$this->contextRepository) {
28       $this->contextRepository = \Drupal::service('context.repository');
29     }
30     return $this->contextRepository;
31   }
32
33   /**
34    * Provides all available contexts, both global and section_storage-specific.
35    *
36    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
37    *   The section storage.
38    *
39    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
40    *   The array of context objects.
41    */
42   protected function getAvailableContexts(SectionStorageInterface $section_storage) {
43     // Get all globally available contexts that have a defined value.
44     $contexts = array_filter($this->contextRepository()->getAvailableContexts(), function (ContextInterface $context) {
45       return $context->hasContextValue();
46     });
47
48     // Add in the per-section_storage contexts.
49     $contexts += $section_storage->getContexts();
50     return $contexts;
51   }
52
53 }