You are here

class CurrentUserContext in Page Manager 8.4

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

Sets the current user as a context.

Hierarchy

  • class \Drupal\page_manager\EventSubscriber\CurrentUserContext implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CurrentUserContext

1 file declares its use of CurrentUserContext
CurrentUserContextTest.php in tests/src/Unit/CurrentUserContextTest.php
1 string reference to 'CurrentUserContext'
page_manager.services.yml in ./page_manager.services.yml
page_manager.services.yml
1 service uses CurrentUserContext
page_manager.current_user_context in ./page_manager.services.yml
Drupal\page_manager\EventSubscriber\CurrentUserContext

File

src/EventSubscriber/CurrentUserContext.php, line 13

Namespace

Drupal\page_manager\EventSubscriber
View source
class CurrentUserContext implements EventSubscriberInterface {

  /**
   * Constructs a new CurrentUserContext.
   *
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
   */
  protected $contextRepository;

  /**
   * Creates LanguageInterfaceContext object.
   *
   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
   *   The context repository service.
   */
  public function __construct(LazyContextRepository $context_repository) {
    $this->contextRepository = $context_repository;
  }

  /**
   * 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) {
    $contexts = $this->contextRepository
      ->getRuntimeContexts([
      '@user.current_user_context:current_user',
    ]);
    $context = reset($contexts);
    $event
      ->getPage()
      ->addContext('@user.current_user_context:current_user', $context)
      ->addContext('current_user', $context);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentUserContext::$contextRepository protected property Constructs a new CurrentUserContext.
CurrentUserContext::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CurrentUserContext::onPageContext public function Adds in the current user as a context.
CurrentUserContext::__construct public function Creates LanguageInterfaceContext object.