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

Breadcrumb

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

function HelpTopicTwigLoader::getSourceContext

Overrides FilesystemLoader::getSourceContext

File

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

Class

HelpTopicTwigLoader
Loads help topic Twig files from the filesystem.

Namespace

Drupal\help

Code

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);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal