You are here

class RouteParamContext in Page Manager 8

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

Sets values from the route parameters as a context.

Hierarchy

Expanded class hierarchy of RouteParamContext

1 file declares its use of RouteParamContext
RouteParamContextTest.php in tests/src/Unit/RouteParamContextTest.php
Contains \Drupal\Tests\page_manager\Unit\RouteParamContextTest.
1 string reference to 'RouteParamContext'
page_manager.services.yml in ./page_manager.services.yml
page_manager.services.yml
1 service uses RouteParamContext
page_manager.route_param_context in ./page_manager.services.yml
Drupal\page_manager\EventSubscriber\RouteParamContext

File

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

Namespace

Drupal\page_manager\EventSubscriber
View source
class RouteParamContext implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a new CurrentUserContext.
   *
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(RouteProviderInterface $route_provider, RequestStack $request_stack) {
    $this->routeProvider = $route_provider;
    $this->requestStack = $request_stack;
  }

  /**
   * Adds in the current user as a context.
   *
   * @param \Drupal\page_manager\Event\PageManagerContextEvent $event
   *   The page entity context event.
   */
  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);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[PageManagerEvents::PAGE_CONTEXT][] = 'onPageContext';
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteParamContext::$requestStack protected property The request stack.
RouteParamContext::$routeProvider protected property The route provider.
RouteParamContext::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RouteParamContext::onPageContext public function Adds in the current user as a context.
RouteParamContext::__construct public function Constructs a new CurrentUserContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.