Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/user/src/ContextProvider/CurrentUserContext.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\user\ContextProvider; | 3 namespace Drupal\user\ContextProvider; |
4 | 4 |
5 use Drupal\Core\Cache\CacheableMetadata; | 5 use Drupal\Core\Cache\CacheableMetadata; |
6 use Drupal\Core\Entity\EntityManagerInterface; | 6 use Drupal\Core\Entity\EntityTypeManagerInterface; |
7 use Drupal\Core\Plugin\Context\ContextProviderInterface; | 7 use Drupal\Core\Plugin\Context\ContextProviderInterface; |
8 use Drupal\Core\Plugin\Context\EntityContext; | 8 use Drupal\Core\Plugin\Context\EntityContext; |
9 use Drupal\Core\Session\AccountInterface; | 9 use Drupal\Core\Session\AccountInterface; |
10 use Drupal\Core\StringTranslation\StringTranslationTrait; | 10 use Drupal\Core\StringTranslation\StringTranslationTrait; |
11 | 11 |
33 /** | 33 /** |
34 * Constructs a new CurrentUserContext. | 34 * Constructs a new CurrentUserContext. |
35 * | 35 * |
36 * @param \Drupal\Core\Session\AccountInterface $account | 36 * @param \Drupal\Core\Session\AccountInterface $account |
37 * The current user. | 37 * The current user. |
38 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | 38 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
39 * The entity manager. | 39 * The entity type manager. |
40 */ | 40 */ |
41 public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) { | 41 public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) { |
42 $this->account = $account; | 42 $this->account = $account; |
43 $this->userStorage = $entity_manager->getStorage('user'); | 43 $this->userStorage = $entity_type_manager->getStorage('user'); |
44 } | 44 } |
45 | 45 |
46 /** | 46 /** |
47 * {@inheritdoc} | 47 * {@inheritdoc} |
48 */ | 48 */ |
51 | 51 |
52 if ($current_user) { | 52 if ($current_user) { |
53 // @todo Do not validate protected fields to avoid bug in TypedData, | 53 // @todo Do not validate protected fields to avoid bug in TypedData, |
54 // remove this in https://www.drupal.org/project/drupal/issues/2934192. | 54 // remove this in https://www.drupal.org/project/drupal/issues/2934192. |
55 $current_user->_skipProtectedUserFieldConstraint = TRUE; | 55 $current_user->_skipProtectedUserFieldConstraint = TRUE; |
56 | |
57 $context = EntityContext::fromEntity($current_user, $this->t('Current user')); | |
58 } | |
59 else { | |
60 // If not user is available, provide an empty context object. | |
61 $context = EntityContext::fromEntityTypeId('user', $this->t('Current user')); | |
56 } | 62 } |
57 | 63 |
58 $context = EntityContext::fromEntity($current_user, $this->t('Current user')); | |
59 $cacheability = new CacheableMetadata(); | 64 $cacheability = new CacheableMetadata(); |
60 $cacheability->setCacheContexts(['user']); | 65 $cacheability->setCacheContexts(['user']); |
61 $context->addCacheableDependency($cacheability); | 66 $context->addCacheableDependency($cacheability); |
62 | 67 |
63 $result = [ | 68 $result = [ |