function LocaleLookup::getCid

Same name in other branches
  1. 9 core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::getCid()
  2. 8.9.x core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::getCid()
  3. 10 core/modules/locale/src/LocaleLookup.php \Drupal\locale\LocaleLookup::getCid()

Overrides CacheCollector::getCid

File

core/modules/locale/src/LocaleLookup.php, line 110

Class

LocaleLookup
A cache collector to allow for dynamic building of the locale cache.

Namespace

Drupal\locale

Code

protected function getCid() {
    if (!isset($this->cid)) {
        // Add the current user's role IDs to the cache key, this ensures that,
        // for example, strings for admin menu items and settings forms are not
        // cached for anonymous users.
        $user = \Drupal::currentUser();
        $rids = '';
        if ($user) {
            $roles = $user->getRoles();
            sort($roles);
            $rids = implode(':', $roles);
        }
        $this->cid = "locale:{$this->langcode}:{$this->context}:{$rids}";
        // Getting the roles from the current user might have resulted in t()
        // calls that attempted to get translations from the locale cache. In that
        // case they would not go into this method again as
        // CacheCollector::lazyLoadCache() already set the loaded flag. They would
        // however call resolveCacheMiss() and add that string to the list of
        // cache misses that need to be written into the cache. Prevent that by
        // resetting that list. All that happens in such a case are a few uncached
        // translation lookups.
        $this->keysToPersist = [];
    }
    return $this->cid;
}

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