class ThemeHandler

Same name in other branches
  1. 9 core/lib/Drupal/Core/Extension/ThemeHandler.php \Drupal\Core\Extension\ThemeHandler
  2. 10 core/lib/Drupal/Core/Extension/ThemeHandler.php \Drupal\Core\Extension\ThemeHandler
  3. 11.x core/lib/Drupal/Core/Extension/ThemeHandler.php \Drupal\Core\Extension\ThemeHandler

Default theme handler using the config system to store installation statuses.

Hierarchy

Expanded class hierarchy of ThemeHandler

2 files declare their use of ThemeHandler
Color.php in core/modules/color/src/Plugin/migrate/source/d7/Color.php
ThemeHandlerTest.php in core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php
Contains \Drupal\Tests\Core\Extension\ThemeHandlerTest.
1 string reference to 'ThemeHandler'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ThemeHandler
theme_handler in core/core.services.yml
Drupal\Core\Extension\ThemeHandler

File

core/lib/Drupal/Core/Extension/ThemeHandler.php, line 12

Namespace

Drupal\Core\Extension
View source
class ThemeHandler implements ThemeHandlerInterface {
    
    /**
     * A list of all currently available themes.
     *
     * @var array
     */
    protected $list;
    
    /**
     * The config factory to get the installed themes.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * An extension discovery instance.
     *
     * @var \Drupal\Core\Extension\ThemeExtensionList
     */
    protected $themeList;
    
    /**
     * The app root.
     *
     * @var string
     */
    protected $root;
    
    /**
     * Constructs a new ThemeHandler.
     *
     * @param string $root
     *   The app root.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory to get the installed themes.
     * @param \Drupal\Core\Extension\ThemeExtensionList $theme_list
     *   A extension discovery instance.
     */
    public function __construct($root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list) {
        $this->root = $root;
        $this->configFactory = $config_factory;
        $this->themeList = $theme_list;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getDefault() {
        return $this->configFactory
            ->get('system.theme')
            ->get('default');
    }
    
    /**
     * {@inheritdoc}
     */
    public function setDefault($name) {
        @trigger_error(__METHOD__ . ' is deprecated in drupal:8.2.0 and is removed from drupal:9.0.0. Use the configuration system to edit the system.theme config directly. See https://www.drupal.org/node/3082630', E_USER_DEPRECATED);
        $list = $this->listInfo();
        if (!isset($list[$name])) {
            throw new UninstalledExtensionException("{$name} theme is not installed.");
        }
        $this->configFactory
            ->getEditable('system.theme')
            ->set('default', $name)
            ->save();
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function install(array $theme_list, $install_dependencies = TRUE) {
        // We keep the old install() method as BC layer but redirect directly to the
        // theme installer.
        @trigger_error('\\Drupal\\Core\\Extension\\ThemeHandlerInterface::install() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \\Drupal\\Core\\Extension\\ThemeInstallerInterface::install() instead. See https://www.drupal.org/node/3017233', E_USER_DEPRECATED);
        return \Drupal::service('theme_installer')->install($theme_list, $install_dependencies);
    }
    
    /**
     * {@inheritdoc}
     */
    public function uninstall(array $theme_list) {
        // We keep the old uninstall() method as BC layer but redirect directly to
        // the theme installer.
        @trigger_error('\\Drupal\\Core\\Extension\\ThemeHandlerInterface::uninstall() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \\Drupal\\Core\\Extension\\ThemeInstallerInterface::uninstall() instead. See https://www.drupal.org/node/3017233', E_USER_DEPRECATED);
        \Drupal::service('theme_installer')->uninstall($theme_list);
    }
    
    /**
     * {@inheritdoc}
     */
    public function listInfo() {
        if (!isset($this->list)) {
            $this->list = [];
            $installed_themes = $this->configFactory
                ->get('core.extension')
                ->get('theme');
            if (!empty($installed_themes)) {
                $installed_themes = array_intersect_key($this->themeList
                    ->getList(), $installed_themes);
                array_map([
                    $this,
                    'addTheme',
                ], $installed_themes);
            }
        }
        return $this->list;
    }
    
    /**
     * {@inheritdoc}
     */
    public function addTheme(Extension $theme) {
        // Register the namespaces of installed themes.
        // @todo Implement proper theme registration
        // https://www.drupal.org/project/drupal/issues/2941757
        \Drupal::service('class_loader')->addPsr4('Drupal\\' . $theme->getName() . '\\', $this->root . '/' . $theme->getPath() . '/src');
        if (!empty($theme->info['libraries'])) {
            foreach ($theme->info['libraries'] as $library => $name) {
                $theme->libraries[$library] = $name;
            }
        }
        if (isset($theme->info['engine'])) {
            $theme->engine = $theme->info['engine'];
        }
        if (isset($theme->info['base theme'])) {
            $theme->base_theme = $theme->info['base theme'];
        }
        $this->list[$theme->getName()] = $theme;
    }
    
    /**
     * {@inheritdoc}
     */
    public function refreshInfo() {
        $installed = $this->configFactory
            ->get('core.extension')
            ->get('theme');
        // Only refresh the info if a theme has been installed. Modules are
        // installed before themes by the installer and this method is called during
        // module installation.
        if (empty($installed) && empty($this->list)) {
            return;
        }
        $this->reset();
    }
    
    /**
     * {@inheritdoc}
     */
    public function reset() {
        $this->themeList
            ->reset();
        $this->list = NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function rebuildThemeData() {
        return $this->themeList
            ->reset()
            ->getList();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getBaseThemes(array $themes, $theme) {
        return $this->themeList
            ->getBaseThemes($themes, $theme);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getName($theme) {
        return $this->themeList
            ->getName($theme);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getThemeDirectories() {
        $dirs = [];
        foreach ($this->listInfo() as $name => $theme) {
            $dirs[$name] = $this->root . '/' . $theme->getPath();
        }
        return $dirs;
    }
    
    /**
     * {@inheritdoc}
     */
    public function themeExists($theme) {
        $themes = $this->listInfo();
        return isset($themes[$theme]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTheme($name) {
        $themes = $this->listInfo();
        if (isset($themes[$name])) {
            return $themes[$name];
        }
        throw new UnknownExtensionException(sprintf('The theme %s does not exist.', $name));
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasUi($name) {
        $themes = $this->listInfo();
        if (isset($themes[$name])) {
            if (!empty($themes[$name]->info['hidden'])) {
                $theme_config = $this->configFactory
                    ->get('system.theme');
                return $name == $theme_config->get('default') || $name == $theme_config->get('admin');
            }
            return TRUE;
        }
        return FALSE;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ThemeHandler::$configFactory protected property The config factory to get the installed themes.
ThemeHandler::$list protected property A list of all currently available themes.
ThemeHandler::$root protected property The app root.
ThemeHandler::$themeList protected property An extension discovery instance.
ThemeHandler::addTheme public function Adds a theme extension to the internal listing. Overrides ThemeHandlerInterface::addTheme
ThemeHandler::getBaseThemes public function Finds all the base themes for the specified theme. Overrides ThemeHandlerInterface::getBaseThemes
ThemeHandler::getDefault public function Returns the default theme. Overrides ThemeHandlerInterface::getDefault
ThemeHandler::getName public function Gets the human readable name of a given theme. Overrides ThemeHandlerInterface::getName
ThemeHandler::getTheme public function Returns a theme extension object from the currently active theme list. Overrides ThemeHandlerInterface::getTheme
ThemeHandler::getThemeDirectories public function Returns an array of directories for all installed themes. Overrides ThemeHandlerInterface::getThemeDirectories
ThemeHandler::hasUi public function Determines if a theme should be shown in the user interface. Overrides ThemeHandlerInterface::hasUi
ThemeHandler::install public function Installs a given list of themes. Overrides ThemeHandlerInterface::install
ThemeHandler::listInfo public function Returns a list of currently installed themes. Overrides ThemeHandlerInterface::listInfo
ThemeHandler::rebuildThemeData public function Scans and collects theme extension data and their engines. Overrides ThemeHandlerInterface::rebuildThemeData
ThemeHandler::refreshInfo public function Refreshes the theme info data of currently installed themes. Overrides ThemeHandlerInterface::refreshInfo
ThemeHandler::reset public function Resets the internal state of the theme handler. Overrides ThemeHandlerInterface::reset
ThemeHandler::setDefault public function Sets a new default theme. Overrides ThemeHandlerInterface::setDefault
ThemeHandler::themeExists public function Determines whether a given theme is installed. Overrides ThemeHandlerInterface::themeExists
ThemeHandler::uninstall public function Uninstalls a given list of themes. Overrides ThemeHandlerInterface::uninstall
ThemeHandler::__construct public function Constructs a new ThemeHandler.

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