function UserRolesCacheContext::getContext

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext()
  2. 8.9.x core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext()
  3. 10 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext()

Overrides CalculatedCacheContextInterface::getContext

File

core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php, line 29

Class

UserRolesCacheContext
Defines the UserRolesCacheContext service, for "per role" caching.

Namespace

Drupal\Core\Cache\Context

Code

public function getContext($role = NULL) {
    // User 1 does not actually have any special behavior for roles; this is
    // added as additional security and backwards compatibility protection for
    // SA-CORE-2015-002.
    // @todo Remove in Drupal 9.0.0.
    if ($this->user
        ->id() == 1) {
        return 'is-super-user';
    }
    if ($role === NULL) {
        return implode(',', $this->user
            ->getRoles());
    }
    else {
        return in_array($role, $this->user
            ->getRoles()) ? 'true' : 'false';
    }
}

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