TwigNodeVisitor.php in Drupal 9
File
core/lib/Drupal/Core/Template/TwigNodeVisitor.php
View source
<?php
namespace Drupal\Core\Template;
use Twig\Environment;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\FunctionExpression;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\NodeVisitor\AbstractNodeVisitor;
class TwigNodeVisitor extends AbstractNodeVisitor {
protected function doEnterNode(Node $node, Environment $env) {
return $node;
}
protected function doLeaveNode(Node $node, Environment $env) {
if ($node instanceof PrintNode) {
if (!empty($this->skipRenderVarFunction)) {
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) {
$node
->getNode('filter')
->setAttribute('value', 'drupal_escape');
$this->skipRenderVarFunction = TRUE;
}
}
return $node;
}
public function getPriority() {
return 256;
}
}
Classes
Name |
Description |
TwigNodeVisitor |
Provides a TwigNodeVisitor to change the generated parse-tree. |