You are here

public function CurrentUserContext::onPageContext in Page Manager 8

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

Adds in the current user as a context.

Parameters

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

File

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

Class

CurrentUserContext
Sets the current user as a context.

Namespace

Drupal\page_manager\EventSubscriber

Code

public function onPageContext(PageManagerContextEvent $event) {
  $id = $this->account
    ->id();
  $current_user = $this->userStorage
    ->load($id);
  $context = new Context(new ContextDefinition('entity:user', $this
    ->t('Current user')), $current_user);
  $cacheability = new CacheableMetadata();
  $cacheability
    ->setCacheContexts([
    'user',
  ]);
  $context
    ->addCacheableDependency($cacheability);
  $event
    ->getPage()
    ->addContext('current_user', $context);
}