function TwigComponentLoader::findTemplate

Same name in other branches
  1. 10 core/modules/sdc/src/Twig/TwigComponentLoader.php \Drupal\sdc\Twig\TwigComponentLoader::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/modules/sdc/src/Twig/TwigComponentLoader.php, line 49

Class

TwigComponentLoader
Lets you load templates using the component ID.

Namespace

Drupal\sdc\Twig

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.