ThemeCacheContext.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/ThemeCacheContext.php
  2. 10 core/lib/Drupal/Core/Cache/Context/ThemeCacheContext.php
  3. 11.x core/lib/Drupal/Core/Cache/Context/ThemeCacheContext.php

Namespace

Drupal\Core\Cache\Context

File

core/lib/Drupal/Core/Cache/Context/ThemeCacheContext.php

View source
<?php

namespace Drupal\Core\Cache\Context;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Theme\ThemeManagerInterface;

/**
 * Defines the ThemeCacheContext service, for "per theme" caching.
 *
 * Cache context ID: 'theme'.
 */
class ThemeCacheContext implements CacheContextInterface {
    
    /**
     * The theme manager.
     *
     * @var \Drupal\Core\Theme\ThemeManagerInterface
     */
    protected $themeManager;
    
    /**
     * Constructs a new ThemeCacheContext service.
     *
     * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
     *   The theme manager.
     */
    public function __construct(ThemeManagerInterface $theme_manager) {
        $this->themeManager = $theme_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Theme');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext() {
        return $this->themeManager
            ->getActiveTheme()
            ->getName() ?: 'stark';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata() {
        return new CacheableMetadata();
    }

}

Classes

Title Deprecated Summary
ThemeCacheContext Defines the ThemeCacheContext service, for "per theme" caching.

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