You are here

protected function LayoutBuilder::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/LayoutBuilder.php, line 77

Class

LayoutBuilder
Exposes contexts available at Layout Builder routes.

Namespace

Drupal\core_context\ContextProvider

Code

protected function getContextsFromRoute(Route $route) : array {
  $entity_type_id = $this
    ->getEntityTypeFromRoute($route);

  // If this is an entity view display, we are editing a default layout.
  // Otherwise, we are editing an entity-specific override and can delegate to
  // the canonical entity provider (albeit by prying its implementation of
  // this method open by reflection).
  if ($entity_type_id === 'entity_view_display') {
    $entity_type_id = $this->routeMatch
      ->getParameter('entity_type_id');

    // If there is no bundle_key parameter, the entity type whose layout we
    // editing does not support bundles.
    $bundle_key = $this->routeMatch
      ->getParameter('bundle_key');
    $bundle = isset($bundle_key) ? $this->routeMatch
      ->getParameter($bundle_key) : $entity_type_id;

    // With certain entity types, such as media items, the bundle is a fully
    // loaded config entity. In such a case, we only need its ID.
    if ($bundle instanceof EntityInterface) {
      $bundle = $bundle
        ->id();
    }
    $view_mode = $this->routeMatch
      ->getParameter('view_mode_name');
    $display = $this->entityDisplayRepository
      ->getViewDisplay($entity_type_id, $bundle, $view_mode);
    return $this
      ->getContextsFromEntity($display);
  }
  else {

    // For now, we can (more or less) assume that 'full' is the canonical
    // view mode.
    // @see \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm::isCanonicalMode()
    $route
      ->setDefault('_core_context_entity', "{$entity_type_id}.full");
    $reflector = new \ReflectionObject($this->canonical);
    $method = $reflector
      ->getMethod(__FUNCTION__);
    $method
      ->setAccessible(TRUE);
    return $method
      ->invoke($this->canonical, $route);
  }
}