You are here

abstract class RouteAwareContextProviderBase in Core Context 8

Provides a base class for context providers which use the current route.

Hierarchy

Expanded class hierarchy of RouteAwareContextProviderBase

File

src/ContextProvider/RouteAwareContextProviderBase.php, line 13

Namespace

Drupal\core_context\ContextProvider
View source
abstract class RouteAwareContextProviderBase extends ContextProviderBase {
  use CacheableContextTrait;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * RouteAwareContextProviderBase constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param mixed ...$arguments
   *   Additional arguments to pass to the parent constructor.
   */
  public function __construct(RouteMatchInterface $route_match, ...$arguments) {
    $this->routeMatch = $route_match;
    parent::__construct(...$arguments);
  }

  /**
   * Determines if this provider can extract contexts from the current route.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route object.
   *
   * @return bool
   *   TRUE if this provider can extract contexts from the route, otherwise
   *   FALSE.
   */
  protected abstract function appliesTo(Route $route) : bool;

  /**
   * Extracts contexts from the current route.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route object.
   *
   * @return \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.
   */
  protected abstract function getContextsFromRoute(Route $route) : array;

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $route = $this->routeMatch
      ->getRouteObject();
    if (empty($route) || $this
      ->appliesTo($route) === FALSE) {
      return [];
    }
    $contexts = $this
      ->getContextsFromRoute($route);
    if ($unqualified_context_ids) {
      $contexts = array_intersect_key($contexts, array_flip($unqualified_context_ids));
    }
    $cache_metadata = new CacheableMetadata();
    $cache_metadata
      ->addCacheContexts([
      'route',
    ]);
    return $this
      ->applyCaching($contexts, $cache_metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    return $this
      ->getRuntimeContexts([]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableContextTrait::applyCaching private function Adds cache metadata to a set of contexts.
ContextProviderBase::$entityTypeManager protected property The entity type manager service.
ContextProviderBase::getContextsFromEntity protected function Extracts contexts from an entity.
RouteAwareContextProviderBase::$routeMatch protected property The route match service.
RouteAwareContextProviderBase::appliesTo abstract protected function Determines if this provider can extract contexts from the current route. 2
RouteAwareContextProviderBase::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
RouteAwareContextProviderBase::getContextsFromRoute abstract protected function Extracts contexts from the current route. 2
RouteAwareContextProviderBase::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
RouteAwareContextProviderBase::__construct public function RouteAwareContextProviderBase constructor. Overrides ContextProviderBase::__construct 2