You are here

class IsCurrentUserPageCacheContext in Opigno dashboard 3.x

Defines a cache context for whether the URL is the current user page.

Cache context ID: 'url.path.is_current_user_page'.

Hierarchy

Expanded class hierarchy of IsCurrentUserPageCacheContext

1 string reference to 'IsCurrentUserPageCacheContext'
opigno_dashboard.services.yml in ./opigno_dashboard.services.yml
opigno_dashboard.services.yml
1 service uses IsCurrentUserPageCacheContext
cache_context.url.path.is_current_user_page in ./opigno_dashboard.services.yml
Drupal\opigno_dashboard\Cache\Context\IsCurrentUserPageCacheContext

File

src/Cache/Context/IsCurrentUserPageCacheContext.php, line 15

Namespace

Drupal\opigno_dashboard\Cache\Context
View source
class IsCurrentUserPageCacheContext implements CacheContextInterface {

  /**
   * If the current page is a user page.
   *
   * @var bool
   */
  protected $isUserPage;

  /**
   * The current user ID.
   *
   * @var int
   */
  protected $uid;

  /**
   * IsCurrentUserPageCacheContext constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The rote match service.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user account.
   */
  public function __construct(RouteMatchInterface $route_match, AccountInterface $account) {
    $this->uid = (int) $account
      ->id();
    $this->isUserPage = $route_match
      ->getRouteName() === 'entity.user.canonical' && (int) $route_match
      ->getRawParameter('user') === $this->uid;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Is current user page');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    return 'is_current_user_page_.' . (int) $this->isUserPage;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    $metadata = new CacheableMetadata();
    $metadata
      ->addCacheTags([
      'user:' . $this->uid,
    ]);
    return $metadata;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IsCurrentUserPageCacheContext::$isUserPage protected property If the current page is a user page.
IsCurrentUserPageCacheContext::$uid protected property The current user ID.
IsCurrentUserPageCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
IsCurrentUserPageCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
IsCurrentUserPageCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
IsCurrentUserPageCacheContext::__construct public function IsCurrentUserPageCacheContext constructor.