You are here

protected function TwigNodeVisitor::doLeaveNode in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Template/TwigNodeVisitor.php \Drupal\Core\Template\TwigNodeVisitor::doLeaveNode()

File

core/lib/Drupal/Core/Template/TwigNodeVisitor.php, line 33

Class

TwigNodeVisitor
Provides a TwigNodeVisitor to change the generated parse-tree.

Namespace

Drupal\Core\Template

Code

protected function doLeaveNode(Node $node, Environment $env) {

  // We use this to inject a call to render_var -> TwigExtension->renderVar()
  // before anything is printed.
  if ($node instanceof PrintNode) {
    if (!empty($this->skipRenderVarFunction)) {

      // No need to add the callback, we have escape active already.
      unset($this->skipRenderVarFunction);
      return $node;
    }
    $class = get_class($node);
    $line = $node
      ->getTemplateLine();
    return new $class(new FunctionExpression('render_var', new Node([
      $node
        ->getNode('expr'),
    ]), $line), $line);
  }
  elseif ($node instanceof FilterExpression) {
    $name = $node
      ->getNode('filter')
      ->getAttribute('value');
    if ('escape' == $name || 'e' == $name) {

      // Use our own escape filter that is MarkupInterface aware.
      $node
        ->getNode('filter')
        ->setAttribute('value', 'drupal_escape');

      // Store that we have a filter active already that knows
      // how to deal with render arrays.
      $this->skipRenderVarFunction = TRUE;
    }
  }
  return $node;
}