function HelpTopicTwigLoader::getSourceContext

Same name and namespace in other branches
  1. 9 core/modules/help_topics/src/HelpTopicTwigLoader.php \Drupal\help_topics\HelpTopicTwigLoader::getSourceContext()
  2. 8.9.x core/modules/help_topics/src/HelpTopicTwigLoader.php \Drupal\help_topics\HelpTopicTwigLoader::getSourceContext()
  3. 11.x core/modules/help/src/HelpTopicTwigLoader.php \Drupal\help\HelpTopicTwigLoader::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);
}

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