You are here

protected function CanonicalEntity::getContextsFromRoute in Core Context 8

Extracts contexts from the current route.

Parameters

\Symfony\Component\Routing\Route $route: The route object.

Return value

\Drupal\Component\Plugin\Context\ContextInterface[] The contexts extracted from the route, keyed by name. Any contexts which can accept cache metadata will get the 'route' cache context applied.

Overrides RouteAwareContextProviderBase::getContextsFromRoute

File

src/ContextProvider/CanonicalEntity.php, line 67

Class

CanonicalEntity
Exposes contexts available at a canonical entity route.

Namespace

Drupal\core_context\ContextProvider

Code

protected function getContextsFromRoute(Route $route) : array {
  list($entity_type_id, $view_mode) = explode('.', $this
    ->getEntityTypeAndViewModeFromRoute($route));
  $entity = $this->routeMatch
    ->getParameter($entity_type_id);
  $contexts = $this
    ->getContextsFromEntity($entity);

  // Only fieldable entities can have entity displays associated with them.
  // We need to check this here in order to avoid raising an exception if
  // the entity view display does not already exist.
  // @see \Drupal\Core\Entity\EntityDisplayBase::__construct()
  if ($entity instanceof FieldableEntityInterface) {
    $display = $this->entityDisplayRepository
      ->getViewDisplay($entity_type_id, $entity
      ->bundle(), $view_mode);
    $contexts = array_merge($contexts, $this
      ->getContextsFromEntity($display));
  }
  return $contexts;
}