You are here

public function RouteParamContext::onPageContext in Page Manager 8

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/RouteParamContext.php \Drupal\page_manager\EventSubscriber\RouteParamContext::onPageContext()

Adds in the current user as a context.

Parameters

\Drupal\page_manager\Event\PageManagerContextEvent $event: The page entity context event.

File

src/EventSubscriber/RouteParamContext.php, line 60
Contains \Drupal\page_manager\EventSubscriber\RouteParamContext.

Class

RouteParamContext
Sets values from the route parameters as a context.

Namespace

Drupal\page_manager\EventSubscriber

Code

public function onPageContext(PageManagerContextEvent $event) {
  $request = $this->requestStack
    ->getCurrentRequest();
  $page = $event
    ->getPage();
  $routes = $this->routeProvider
    ->getRoutesByPattern($page
    ->getPath())
    ->all();
  $route = reset($routes);
  if ($route && ($route_contexts = $route
    ->getOption('parameters'))) {
    foreach ($route_contexts as $route_context_name => $route_context) {

      // Skip this parameter.
      if ($route_context_name == 'page_manager_page_variant' || $route_context_name == 'page_manager_page') {
        continue;
      }
      $parameter = $page
        ->getParameter($route_context_name);
      $context_name = !empty($parameter['label']) ? $parameter['label'] : $this
        ->t('{@name} from route', [
        '@name' => $route_context_name,
      ]);
      if ($request->attributes
        ->has($route_context_name)) {
        $value = $request->attributes
          ->get($route_context_name);
      }
      else {
        $value = NULL;
      }
      $cacheability = new CacheableMetadata();
      $cacheability
        ->setCacheContexts([
        'route',
      ]);
      $context = new Context(new ContextDefinition($route_context['type'], $context_name, FALSE), $value);
      $context
        ->addCacheableDependency($cacheability);
      $page
        ->addContext($route_context_name, $context);
    }
  }
}