class CurrentUserContext in Page Manager 8
Same name and namespace in other branches
- 8.4 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 uses StringTranslationTrait
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'
1 service uses CurrentUserContext
File
- src/
EventSubscriber/ CurrentUserContext.php, line 23 - Contains \Drupal\page_manager\EventSubscriber\CurrentUserContext.
Namespace
Drupal\page_manager\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrentUserContext:: |
protected | property | The account proxy. | |
CurrentUserContext:: |
protected | property | The user storage. | |
CurrentUserContext:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CurrentUserContext:: |
public | function | Adds in the current user as a context. | |
CurrentUserContext:: |
public | function | Constructs a new CurrentUserContext. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |