You are here

public function Page::getContexts in Page Manager 8.4

Same name and namespace in other branches
  1. 8 src/Entity/Page.php \Drupal\page_manager\Entity\Page::getContexts()

Gets the values for all defined contexts.

Return value

\Drupal\Core\Plugin\Context\ContextInterface[] An array of set context values, keyed by context name.

Overrides PageInterface::getContexts

File

src/Entity/Page.php, line 341

Class

Page
Defines a Page entity class.

Namespace

Drupal\page_manager\Entity

Code

public function getContexts() {

  // @todo add the other global contexts here as they are added
  // @todo maybe come up with a non-hardcoded way of doing this?
  $global_contexts = [
    'current_user',
  ];
  if (!$this->contexts) {
    $this
      ->eventDispatcher()
      ->dispatch(PageManagerEvents::PAGE_CONTEXT, new PageManagerContextEvent($this));
    foreach ($this
      ->getParameters() as $machine_name => $configuration) {

      // Parameters can be updated in the UI, so unless it's a global context
      // we'll need to rely on the current settings in the tempstore instead
      // of the ones cached in the router.
      if (!isset($global_contexts[$machine_name])) {

        // First time through, parameters will not be defined by the route.
        if (!isset($this->contexts[$machine_name])) {
          $cacheability = new CacheableMetadata();
          $cacheability
            ->setCacheContexts([
            'route',
          ]);
          $context_definition = ContextDefinitionFactory::create($configuration['type'])
            ->setLabel($configuration['label']);
          $context = new Context($context_definition);
          $context
            ->addCacheableDependency($cacheability);
          $this->contexts[$machine_name] = $context;
        }
        else {
          $this->contexts[$machine_name]
            ->getContextDefinition()
            ->setDataType($configuration['type']);
          if (!empty($configuration['label'])) {
            $this->contexts[$machine_name]
              ->getContextDefinition()
              ->setLabel($configuration['label']);
          }
        }
      }
    }
  }
  return $this->contexts;
}