function ComponentLoader::findTemplate

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Template/Loader/ComponentLoader.php \Drupal\Core\Template\Loader\ComponentLoader::findTemplate()

Finds a template in the file system based on the template name.

Parameters

string $name: The template name.

bool $throw: TRUE to throw an exception when the component is not found. FALSE to return NULL if the component cannot be found.

Return value

string|null The path to the component.

Throws

\Twig\Error\LoaderError Thrown if a template matching $name cannot be found and $throw is TRUE.

File

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

Class

ComponentLoader
Lets you load templates using the component ID.

Namespace

Drupal\Core\Template\Loader

Code

protected function findTemplate(string $name, bool $throw = TRUE) : ?string {
    $path = $name;
    try {
        $component = $this->pluginManager
            ->find($name);
        $path = $component->getTemplatePath();
    } catch (ComponentNotFoundException $e) {
        if ($throw) {
            throw new LoaderError($e->getMessage(), $e->getCode(), $e);
        }
    }
    if ($path || !$throw) {
        return $path;
    }
    throw new LoaderError(sprintf('Unable to find template "%s" in the components registry.', $name));
}

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