annotate core/modules/layout_builder/src/Context/LayoutBuilderContextTrait.php @ 5:12f9dff5fda9 tip

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