class ComponentLoader

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Template/Loader/ComponentLoader.php \Drupal\Core\Template\Loader\ComponentLoader
  2. 10 core/lib/Drupal/Core/Template/Loader/ComponentLoader.php \Drupal\Core\Template\Loader\ComponentLoader

Lets you load templates using the component ID.

Hierarchy

  • class \Drupal\Core\Template\Loader\ComponentLoader implements \Twig\Loader\LoaderInterface

Expanded class hierarchy of ComponentLoader

1 file declares its use of ComponentLoader
ComponentLoaderTest.php in core/tests/Drupal/Tests/Core/Theme/Component/ComponentLoaderTest.php

File

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

Namespace

Drupal\Core\Template\Loader
View source
class ComponentLoader implements LoaderInterface {
  
  /**
   * Constructs a new ComponentLoader object.
   *
   * @param \Drupal\Core\Theme\ComponentPluginManager $pluginManager
   *   The plugin manager.
   */
  public function __construct(protected ComponentPluginManager $pluginManager) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function exists($name) : bool {
    if (!preg_match('/^[a-zA-Z][a-zA-Z0-9:_-]*[a-zA-Z0-9]?$/', $name)) {
      return FALSE;
    }
    try {
      $this->pluginManager
        ->find($name);
      return TRUE;
    } catch (ComponentNotFoundException) {
      return FALSE;
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSourceContext($name) : Source {
    try {
      $component = $this->pluginManager
        ->find($name);
      $path = $component->getTemplatePath();
    } catch (ComponentNotFoundException) {
      return new Source('', $name, '');
    }
    $original_code = file_get_contents($path);
    return new Source($original_code, $name, $path);
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheKey($name) : string {
    try {
      $component = $this->pluginManager
        ->find($name);
    } catch (ComponentNotFoundException) {
      throw new LoaderError('Unable to find component');
    }
    return implode('--', array_filter([
      'components',
      $name,
      $component->getPluginDefinition()['provider'] ?? '',
    ]));
  }
  
  /**
   * {@inheritdoc}
   */
  public function isFresh(string $name, int $time) : bool {
    $file_is_fresh = static fn(string $path) => filemtime($path) < $time;
    try {
      $component = $this->pluginManager
        ->find($name);
    } catch (ComponentNotFoundException) {
      throw new LoaderError('Unable to find component');
    }
    $metadata_path = $component->getPluginDefinition()[YamlDirectoryDiscovery::FILE_KEY];
    return $file_is_fresh($component->getTemplatePath()) && $file_is_fresh($metadata_path);
  }

}

Members

Title Sort descending Modifiers Object type Summary
ComponentLoader::exists public function
ComponentLoader::getCacheKey public function
ComponentLoader::getSourceContext public function
ComponentLoader::isFresh public function
ComponentLoader::__construct public function Constructs a new ComponentLoader object.

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