function TwigNodeVisitor::leaveNode

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Template/TwigNodeVisitor.php \Drupal\Core\Template\TwigNodeVisitor::leaveNode()

File

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

Class

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

Namespace

Drupal\Core\Template

Code

public function leaveNode(Node $node, Environment $env) : ?Node {
    // 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(new TwigFunction('render_var', [
            $env->getExtension(TwigExtension::class),
            'renderVar',
        ]), new Node([
            $node->getNode('expr'),
        ]), $line), $line);
    }
    elseif ($node instanceof FilterExpression) {
        $name = $node->getAttribute('twig_callable')
            ->getName();
        if ('escape' == $name || 'e' == $name) {
            // Use our own escape filter that is MarkupInterface aware.
            $node->setAttribute('twig_callable', $env->getFilter('drupal_escape'));
            // Store that we have a filter active already that knows
            // how to deal with render arrays.
            $this->skipRenderVarFunction = TRUE;
        }
    }
    return $node;
}

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