Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. HelpTopicTwigLoader.php

class HelpTopicTwigLoader

Loads help topic Twig files from the filesystem.

This loader adds module and theme help topic paths to a help_topics namespace to the Twig filesystem loader so that help_topics can be referenced, using '@help-topic/pluginId.html.twig'.

@internal Tagged services are internal.

Hierarchy

  • class \Twig\Loader\FilesystemLoader implements \Twig\Loader\LoaderInterface
    • class \Drupal\help\HelpTopicTwigLoader extends \Twig\Loader\FilesystemLoader

Expanded class hierarchy of HelpTopicTwigLoader

See also

\Drupal\help\HelpTopicDiscovery

\Drupal\help\HelpTopicTwig

1 string reference to 'HelpTopicTwigLoader'
help.services.yml in core/modules/help/help.services.yml
core/modules/help/help.services.yml
1 service uses HelpTopicTwigLoader
help.twig.loader in core/modules/help/help.services.yml
Drupal\help\HelpTopicTwigLoader

File

core/modules/help/src/HelpTopicTwigLoader.php, line 27

Namespace

Drupal\help
View source
class HelpTopicTwigLoader extends FilesystemLoader {
    
    /**
     * {@inheritdoc}
     */
    const MAIN_NAMESPACE = 'help_topics';
    
    /**
     * Constructs a new HelpTopicTwigLoader object.
     *
     * @param string $root_path
     *   The root path.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler service.
     * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
     *   The theme handler service.
     */
    public function __construct($root_path, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
        parent::__construct([], $root_path);
        // Add help_topics directories for modules and themes in the 'help_topic'
        // namespace, plus core.
        $this->addExtension($root_path . '/core');
        array_map([
            $this,
            'addExtension',
        ], $module_handler->getModuleDirectories());
        array_map([
            $this,
            'addExtension',
        ], $theme_handler->getThemeDirectories());
    }
    
    /**
     * Adds an extensions help_topics directory to the Twig loader.
     *
     * @param $path
     *   The path to the extension.
     */
    protected function addExtension($path) {
        $path .= DIRECTORY_SEPARATOR . 'help_topics';
        if (is_dir($path)) {
            $this->cache = $this->errorCache = [];
            $this->paths[self::MAIN_NAMESPACE][] = rtrim($path, '/\\');
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSourceContext(string $name) : Source {
        $path = $this->findTemplate($name);
        $contents = file_get_contents($path);
        try {
            // Note: always use \Drupal\Core\Serialization\Yaml here instead of the
            // "serializer.yaml" service. This allows the core serializer to utilize
            // core related functionality which isn't available as the standalone
            // component based serializer.
            $front_matter = new FrontMatter($contents, Yaml::class);
            // Reconstruct the content if there is front matter data detected. Prepend
            // the source with {% line \d+ %} to inform Twig that the source code
            // actually starts on a different line past the front matter data. This is
            // particularly useful when used in error reporting.
            if ($front_matter->getData() && ($line = $front_matter->getLine())) {
                $contents = "{% line {$line} %}" . $front_matter->getContent();
            }
        } catch (InvalidDataTypeException $e) {
            throw new LoaderError(sprintf('Malformed YAML in help topic "%s": %s.', $path, $e->getMessage()));
        }
        return new Source($contents, $name, $path);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function findTemplate($name, $throw = TRUE) {
        if (!str_ends_with($name, '.html.twig')) {
            if (!$throw) {
                return NULL;
            }
            $extension = pathinfo($name, PATHINFO_EXTENSION);
            throw new LoaderError(sprintf("Help topic %s has an invalid file extension (%s). Only help topics ending .html.twig are allowed.", $name, $extension));
        }
        return parent::findTemplate($name, $throw);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FilesystemLoader::$cache protected property
FilesystemLoader::$errorCache protected property
FilesystemLoader::$paths protected property
FilesystemLoader::$rootPath private property
FilesystemLoader::addPath public function 1
FilesystemLoader::exists public function Overrides LoaderInterface::exists
FilesystemLoader::getCacheKey public function Gets the cache key to use for the cache for a given template name. Overrides LoaderInterface::getCacheKey 1
FilesystemLoader::getNamespaces public function Returns the path namespaces.
FilesystemLoader::getPaths public function Returns the paths to the templates.
FilesystemLoader::isAbsolutePath private function
FilesystemLoader::isFresh public function Overrides LoaderInterface::isFresh
FilesystemLoader::normalizeName private function
FilesystemLoader::parseName private function
FilesystemLoader::prependPath public function
FilesystemLoader::setPaths public function
FilesystemLoader::validateName private function
HelpTopicTwigLoader::addExtension protected function Adds an extensions help_topics directory to the Twig loader.
HelpTopicTwigLoader::findTemplate protected function Overrides FilesystemLoader::findTemplate
HelpTopicTwigLoader::getSourceContext public function Returns the source context for a given template logical name. Overrides FilesystemLoader::getSourceContext
HelpTopicTwigLoader::MAIN_NAMESPACE constant Identifier of the main namespace. Overrides FilesystemLoader::MAIN_NAMESPACE
HelpTopicTwigLoader::__construct public function Constructs a new HelpTopicTwigLoader object. Overrides FilesystemLoader::__construct
RSS feed
Powered by Drupal