3 namespace Drupal\layout_builder\Context;
5 use Drupal\Core\Plugin\Context\ContextInterface;
6 use Drupal\layout_builder\SectionStorageInterface;
9 * Provides a wrapper around getting contexts from a section storage object.
11 trait LayoutBuilderContextTrait {
14 * The context repository.
16 * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
18 protected $contextRepository;
21 * Gets the context repository service.
23 * @return \Drupal\Core\Plugin\Context\ContextRepositoryInterface
24 * The context repository service.
26 protected function contextRepository() {
27 if (!$this->contextRepository) {
28 $this->contextRepository = \Drupal::service('context.repository');
30 return $this->contextRepository;
34 * Provides all available contexts, both global and section_storage-specific.
36 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
37 * The section storage.
39 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
40 * The array of context objects.
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();
48 // Add in the per-section_storage contexts.
49 $contexts += $section_storage->getContexts();