function HelpTestTwigNodeVisitor::leaveNode

Same name in other branches
  1. 11.x core/modules/help/tests/modules/help_topics_twig_tester/src/HelpTestTwigNodeVisitor.php \Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor::leaveNode()

File

core/modules/help/tests/modules/help_topics_twig_tester/src/HelpTestTwigNodeVisitor.php, line 42

Class

HelpTestTwigNodeVisitor
Defines a Twig node visitor for testing help topics.

Namespace

Drupal\help_topics_twig_tester

Code

public function leaveNode(Node $node, Environment $env) : ?Node {
    $processing = static::getState();
    if (!$processing['manner']) {
        return $node;
    }
    // For all special processing, we want to remove variables, set statements,
    // and assorted Twig expression calls (if, do, etc.).
    if ($node instanceof SetNode || $node instanceof PrintNode || $node instanceof AbstractExpression) {
        return NULL;
    }
    if ($node instanceof TwigNodeTrans) {
        // Count the number of translated chunks.
        $this_chunk = $processing['chunk_count'] + 1;
        static::setStateValue('chunk_count', $this_chunk);
        if ($this_chunk > $processing['max_chunk']) {
            static::setStateValue('max_chunk', $this_chunk);
        }
        if ($processing['manner'] == 'remove_translated') {
            // Remove all translated text.
            return NULL;
        }
        elseif ($processing['manner'] == 'replace_translated') {
            // Replace with a dummy string.
            $node = new TextNode('dummy', 0);
        }
        elseif ($processing['manner'] == 'translated_chunk') {
            // Return the text only if it's the next chunk we're supposed to return.
            // Add a wrapper, because non-translated nodes will still be returned.
            if ($this_chunk == $processing['return_chunk']) {
                return new TextNode(static::DELIMITER . $this->extractText($node) . static::DELIMITER, 0);
            }
            else {
                return NULL;
            }
        }
    }
    if ($processing['manner'] == 'remove_translated' && $node instanceof TextNode) {
        // For this processing, we also want to remove all HTML tags and
        // whitespace from TextNodes.
        $text = $node->getAttribute('data');
        $text = strip_tags($text);
        $text = preg_replace('|\\s+|', '', $text);
        return new TextNode($text, 0);
    }
    return $node;
}

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