Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/ContextProvider/CurrentUserContext.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\ContextProvider; | |
4 | |
5 use Drupal\Core\Cache\CacheableMetadata; | |
6 use Drupal\Core\Entity\EntityManagerInterface; | |
7 use Drupal\Core\Plugin\Context\Context; | |
8 use Drupal\Core\Plugin\Context\ContextDefinition; | |
9 use Drupal\Core\Plugin\Context\ContextProviderInterface; | |
10 use Drupal\Core\Session\AccountInterface; | |
11 use Drupal\Core\StringTranslation\StringTranslationTrait; | |
12 | |
13 /** | |
14 * Sets the current user as a context. | |
15 */ | |
16 class CurrentUserContext implements ContextProviderInterface { | |
17 | |
18 use StringTranslationTrait; | |
19 | |
20 /** | |
21 * The current user. | |
22 * | |
23 * @var \Drupal\Core\Session\AccountInterface | |
24 */ | |
25 protected $account; | |
26 | |
27 /** | |
28 * The user storage. | |
29 * | |
30 * @var \Drupal\user\UserStorageInterface | |
31 */ | |
32 protected $userStorage; | |
33 | |
34 /** | |
35 * Constructs a new CurrentUserContext. | |
36 * | |
37 * @param \Drupal\Core\Session\AccountInterface $account | |
38 * The current user. | |
39 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | |
40 * The entity manager. | |
41 */ | |
42 public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) { | |
43 $this->account = $account; | |
44 $this->userStorage = $entity_manager->getStorage('user'); | |
45 } | |
46 | |
47 /** | |
48 * {@inheritdoc} | |
49 */ | |
50 public function getRuntimeContexts(array $unqualified_context_ids) { | |
51 $current_user = $this->userStorage->load($this->account->id()); | |
52 | |
53 $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user); | |
54 $cacheability = new CacheableMetadata(); | |
55 $cacheability->setCacheContexts(['user']); | |
56 $context->addCacheableDependency($cacheability); | |
57 | |
58 $result = [ | |
59 'current_user' => $context, | |
60 ]; | |
61 | |
62 return $result; | |
63 } | |
64 | |
65 /** | |
66 * {@inheritdoc} | |
67 */ | |
68 public function getAvailableContexts() { | |
69 return $this->getRuntimeContexts([]); | |
70 } | |
71 | |
72 } |