function HelpTestTwigNodeVisitor::extractText

Same name and namespace in other branches
  1. 9 core/modules/help_topics/tests/modules/help_topics_twig_tester/src/HelpTestTwigNodeVisitor.php \Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor::extractText()
  2. 11.x core/modules/help/tests/modules/help_topics_twig_tester/src/HelpTestTwigNodeVisitor.php \Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor::extractText()

Extracts the text from a translated text object.

Parameters

\Drupal\Core\Template\TwigNodeTrans $node: Translated text node.

Return value

string Text in the node.

1 call to HelpTestTwigNodeVisitor::extractText()
HelpTestTwigNodeVisitor::leaveNode in core/modules/help/tests/modules/help_topics_twig_tester/src/HelpTestTwigNodeVisitor.php

File

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

Class

HelpTestTwigNodeVisitor
Defines a Twig node visitor for testing help topics.

Namespace

Drupal\help_topics_twig_tester

Code

protected function extractText(TwigNodeTrans $node) {
  // Extract the singular/body and optional plural text from the
  // TwigNodeTrans object.
  $bodies = $node->getNode('body');
  if (!count($bodies)) {
    $bodies = [
      $bodies,
    ];
  }
  if ($node->hasNode('plural')) {
    $plural = $node->getNode('plural');
    if (!count($plural)) {
      $bodies[] = $plural;
    }
    else {
      foreach ($plural as $item) {
        $bodies[] = $item;
      }
    }
  }
  // Extract the text from each component of the singular/plural strings.
  $text = '';
  foreach ($bodies as $body) {
    if ($body->hasAttribute('data')) {
      $text .= $body->getAttribute('data');
    }
  }
  return trim($text);
}

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