class ThemeRegistryLoader

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

Loads templates based on information from the Drupal theme registry.

Allows for template inheritance based on the currently active template.

Hierarchy

  • class \Drupal\Core\Template\Loader\ThemeRegistryLoader extends \Drupal\Core\Template\Loader\Twig_Loader_Filesystem

Expanded class hierarchy of ThemeRegistryLoader

1 file declares its use of ThemeRegistryLoader
ThemeRegistryLoaderTest.php in core/tests/Drupal/Tests/Core/Template/Loader/ThemeRegistryLoaderTest.php
1 string reference to 'ThemeRegistryLoader'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ThemeRegistryLoader
twig.loader.theme_registry in core/core.services.yml
Drupal\Core\Template\Loader\ThemeRegistryLoader

File

core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php, line 12

Namespace

Drupal\Core\Template\Loader
View source
class ThemeRegistryLoader extends \Twig_Loader_Filesystem {
    
    /**
     * The theme registry used to determine which template to use.
     *
     * @var \Drupal\Core\Theme\Registry
     */
    protected $themeRegistry;
    
    /**
     * Constructs a new ThemeRegistryLoader object.
     *
     * @param \Drupal\Core\Theme\Registry $theme_registry
     *   The theme registry.
     */
    public function __construct(Registry $theme_registry) {
        $this->themeRegistry = $theme_registry;
    }
    
    /**
     * Finds the path to the requested template.
     *
     * @param string $name
     *   The name of the template to load.
     * @param bool $throw
     *   Whether to throw an exception when an error occurs.
     *
     * @return string|false
     *   The path to the template, or false if the template is not found.
     *
     * @throws \Twig_Error_Loader
     *   Thrown if a template matching $name cannot be found.
     */
    protected function findTemplate($name, $throw = TRUE) {
        // Allow for loading based on the Drupal theme registry.
        $hook = str_replace('.html.twig', '', strtr($name, '-', '_'));
        $theme_registry = $this->themeRegistry
            ->getRuntime();
        if ($theme_registry->has($hook)) {
            $info = $theme_registry->get($hook);
            if (isset($info['path'])) {
                $path = $info['path'] . '/' . $name;
            }
            elseif (isset($info['template'])) {
                $path = $info['template'] . '.html.twig';
            }
            if (isset($path) && is_file($path)) {
                return $this->cache[$name] = $path;
            }
        }
        if ($throw) {
            throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));
        }
        return FALSE;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ThemeRegistryLoader::$themeRegistry protected property The theme registry used to determine which template to use.
ThemeRegistryLoader::findTemplate protected function Finds the path to the requested template.
ThemeRegistryLoader::__construct public function Constructs a new ThemeRegistryLoader object.

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