You are here

class ComponentsLoader in Components! 3.x

Same name and namespace in other branches
  1. 8.2 src/Template/Loader/ComponentsLoader.php \Drupal\components\Template\Loader\ComponentsLoader

Loads namespaced templates from the filesystem.

This loader adds module and theme defined namespaces to the Twig filesystem loader so that templates can be referenced by namespace, like @mycomponents/box.html.twig or @mythemeComponents/page.html.twig.

Hierarchy

  • class \Drupal\components\Template\Loader\ComponentsLoader extends \Twig\Loader\FilesystemLoader

Expanded class hierarchy of ComponentsLoader

1 file declares its use of ComponentsLoader
ComponentsLoaderTest.php in tests/src/Unit/ComponentsLoaderTest.php
1 string reference to 'ComponentsLoader'
components.services.yml in ./components.services.yml
components.services.yml
1 service uses ComponentsLoader
components.twig.loader in ./components.services.yml
Drupal\components\Template\Loader\ComponentsLoader

File

src/Template/Loader/ComponentsLoader.php, line 16

Namespace

Drupal\components\Template\Loader
View source
class ComponentsLoader extends FilesystemLoader {

  /**
   * The components registry service.
   *
   * @var \Drupal\components\Template\ComponentsRegistry
   */
  protected $componentsRegistry;

  /**
   * Constructs a new ComponentsLoader object.
   *
   * @param \Drupal\components\Template\ComponentsRegistry $componentsRegistry
   *   The components registry service.
   */
  public function __construct(ComponentsRegistry $componentsRegistry) {
    parent::__construct();
    $this->componentsRegistry = $componentsRegistry;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Twig\Error\LoaderError
   *   Thrown if a template matching $name cannot be found.
   */
  protected function findTemplate($name, $throw = TRUE) {

    // Validate the given template.
    $extension = substr($name, strrpos($name, '.', -1));
    if ($name[0] !== '@' || !str_contains(substr($name, 2), '/') || $extension !== '.twig' && $extension !== '.html' && $extension !== '.svg') {
      if (!$throw) {
        return NULL;
      }
      throw new LoaderError(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name.twig").', $name));
    }
    else {

      // componentsRegistry::getTemplate() returns a string or NULL, exactly
      // what componentsLoader::findTemplate() should return.
      $path = $this->componentsRegistry
        ->getTemplate($name);
      if ($path || !$throw) {
        return $path;
      }
      throw new LoaderError(sprintf('Unable to find template "%s" in the components registry.', $name));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function exists($name) : bool {
    return (bool) $this->componentsRegistry
      ->getTemplate($name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComponentsLoader::$componentsRegistry protected property The components registry service.
ComponentsLoader::exists public function
ComponentsLoader::findTemplate protected function
ComponentsLoader::__construct public function Constructs a new ComponentsLoader object.