function ThemeRegistryLoader::findTemplate

Same name in other branches
  1. 9 core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php \Drupal\Core\Template\Loader\ThemeRegistryLoader::findTemplate()
  2. 8.9.x core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php \Drupal\Core\Template\Loader\ThemeRegistryLoader::findTemplate()
  3. 11.x core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php \Drupal\Core\Template\Loader\ThemeRegistryLoader::findTemplate()

Finds the path to the requested template.

Parameters

string $name: The name of the template to load.

bool $throw: Whether to throw an exception when an error occurs.

Return value

string|null The path to the template, or NULL if the template is not found.

Throws

\Twig\Error\LoaderError Thrown if a template matching $name cannot be found.

1 call to ThemeRegistryLoader::findTemplate()
ThemeRegistryLoader::getCacheKey in core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php

File

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

Class

ThemeRegistryLoader
Loads templates based on information from the Drupal theme registry.

Namespace

Drupal\Core\Template\Loader

Code

protected function findTemplate(string $name, bool $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 LoaderError(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));
    }
    return NULL;
}

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