class CurrentUserContext

Same name and namespace in other branches
  1. 9 core/modules/user/src/ContextProvider/CurrentUserContext.php \Drupal\user\ContextProvider\CurrentUserContext
  2. 8.9.x core/modules/user/src/ContextProvider/CurrentUserContext.php \Drupal\user\ContextProvider\CurrentUserContext
  3. 11.x core/modules/user/src/ContextProvider/CurrentUserContext.php \Drupal\user\ContextProvider\CurrentUserContext

Sets the current user as a context.

Hierarchy

  • class \Drupal\user\ContextProvider\CurrentUserContext implements \Drupal\Core\Plugin\Context\ContextProviderInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of CurrentUserContext

1 string reference to 'CurrentUserContext'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses CurrentUserContext
user.current_user_context in core/modules/user/user.services.yml
Drupal\user\ContextProvider\CurrentUserContext

File

core/modules/user/src/ContextProvider/CurrentUserContext.php, line 15

Namespace

Drupal\user\ContextProvider
View source
class CurrentUserContext implements ContextProviderInterface {
    use StringTranslationTrait;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $account;
    
    /**
     * The user storage.
     *
     * @var \Drupal\user\UserStorageInterface
     */
    protected $userStorage;
    
    /**
     * Constructs a new CurrentUserContext.
     *
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The current user.
     * @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');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRuntimeContexts(array $unqualified_context_ids) {
        $current_user = $this->userStorage
            ->load($this->account
            ->id());
        if ($current_user) {
            // @todo Do not validate protected fields to avoid bug in TypedData,
            //   remove this in https://www.drupal.org/project/drupal/issues/2934192.
            $current_user->_skipProtectedUserFieldConstraint = TRUE;
            $context = EntityContext::fromEntity($current_user, $this->t('Current user'));
        }
        else {
            // If not user is available, provide an empty context object.
            $context = EntityContext::fromEntityTypeId('user', $this->t('Current user'));
        }
        $cacheability = new CacheableMetadata();
        $cacheability->setCacheContexts([
            'user',
        ]);
        $context->addCacheableDependency($cacheability);
        $result = [
            'current_user' => $context,
        ];
        return $result;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAvailableContexts() {
        return $this->getRuntimeContexts([]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CurrentUserContext::$account protected property The current user.
CurrentUserContext::$userStorage protected property The user storage.
CurrentUserContext::getAvailableContexts public function Overrides ContextProviderInterface::getAvailableContexts
CurrentUserContext::getRuntimeContexts public function Overrides ContextProviderInterface::getRuntimeContexts
CurrentUserContext::__construct public function Constructs a new CurrentUserContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.