You are here

class CurrentUserContext in Page Manager 8

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

Sets the current user as a context.

Hierarchy

Expanded class hierarchy of CurrentUserContext

1 file declares its use of CurrentUserContext
CurrentUserContextTest.php in tests/src/Unit/CurrentUserContextTest.php
Contains \Drupal\Tests\page_manager\Unit\CurrentUserContextTest.
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 23
Contains \Drupal\page_manager\EventSubscriber\CurrentUserContext.

Namespace

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

  /**
   * The account proxy.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * Constructs a new CurrentUserContext.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current account.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) {
    $this->account = $account;
    $this->userStorage = $entity_type_manager
      ->getStorage('user');
  }

  /**
   * 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) {
    $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);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentUserContext::$account protected property The account proxy.
CurrentUserContext::$userStorage protected property The user storage.
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 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.