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