You are here

public function UserRolesCacheContext::getContext in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext()
  2. 9 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext::getContext()

Returns the string representation of the cache context.

A cache context service's name is used as a token (placeholder) cache key, and is then replaced with the string returned by this method.

Parameters

string|null $parameter: The parameter, or NULL to indicate all possible parameter values.

Return value

string The string representation of the cache context. When $parameter is NULL, a value representing all possible parameters must be generated.

Throws

\LogicException Thrown if the passed in parameter is invalid.

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()) ? '0' : '1';
  }
}