You are here

public function HelpTopicTwigLoader::getSourceContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/help_topics/src/HelpTopicTwigLoader.php \Drupal\help_topics\HelpTopicTwigLoader::getSourceContext()

File

core/modules/help_topics/src/HelpTopicTwigLoader.php, line 72

Class

HelpTopicTwigLoader
Loads help topic Twig files from the filesystem.

Namespace

Drupal\help_topics

Code

public function getSourceContext($name) {
  $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);
}