You are here

class RouteIsAdminCacheContext in Menu Per Role 8

Defines a cache context depending if the route is an admin route or not.

Hierarchy

Expanded class hierarchy of RouteIsAdminCacheContext

1 string reference to 'RouteIsAdminCacheContext'
menu_per_role.services.yml in ./menu_per_role.services.yml
menu_per_role.services.yml
1 service uses RouteIsAdminCacheContext
cache_context.route.is_admin in ./menu_per_role.services.yml
Drupal\menu_per_role\Cache\Context\RouteIsAdminCacheContext

File

src/Cache/Context/RouteIsAdminCacheContext.php, line 14

Namespace

Drupal\menu_per_role\Cache\Context
View source
class RouteIsAdminCacheContext implements CacheContextInterface {

  /**
   * Context ID.
   *
   * @const string
   */
  const CONTEXT_ID = 'route.is_admin';

  /**
   * The router admin context service.
   *
   * @var \Drupal\Core\Routing\AdminContext
   */
  protected $adminContext;

  /**
   * RouteIsAdminCacheContext constructor.
   *
   * @param \Drupal\Core\Routing\AdminContext $adminContext
   *   The router admin context service.
   */
  public function __construct(AdminContext $adminContext) {
    $this->adminContext = $adminContext;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Route is admin');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    return $this->adminContext
      ->isAdminRoute() ? '1' : '0';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteIsAdminCacheContext::$adminContext protected property The router admin context service.
RouteIsAdminCacheContext::CONTEXT_ID constant Context ID.
RouteIsAdminCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
RouteIsAdminCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
RouteIsAdminCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
RouteIsAdminCacheContext::__construct public function RouteIsAdminCacheContext constructor.