Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Template/TwigNodeTrans.php \Drupal\Core\Template\TwigNodeTrans::compile()
  2. 9 core/lib/Drupal/Core/Template/TwigNodeTrans.php \Drupal\Core\Template\TwigNodeTrans::compile()

File

core/lib/Drupal/Core/Template/TwigNodeTrans.php, line 52

Class

TwigNodeTrans

Namespace

Drupal\Core\Template

Code

public function compile(Compiler $compiler) {
  $compiler
    ->addDebugInfo($this);
  [
    $singular,
    $tokens,
  ] = $this
    ->compileString($this
    ->getNode('body'));
  $plural = NULL;
  if ($this
    ->hasNode('plural')) {
    [
      $plural,
      $pluralTokens,
    ] = $this
      ->compileString($this
      ->getNode('plural'));
    $tokens = array_merge($tokens, $pluralTokens);
  }

  // Start writing with the function to be called.
  $compiler
    ->write('yield ' . (empty($plural) ? 't' : '\\Drupal::translation()->formatPlural') . '(');

  // Move the count to the beginning of the parameters list.
  if (!empty($plural)) {
    $compiler
      ->raw('abs(')
      ->subcompile($this
      ->getNode('count'))
      ->raw('), ');
  }

  // Write the singular text parameter.
  $compiler
    ->subcompile($singular);

  // Write the plural text parameter, if necessary.
  if (!empty($plural)) {
    $compiler
      ->raw(', ')
      ->subcompile($plural);
  }

  // Write any tokens found as an associative array parameter, otherwise just
  // leave as an empty array.
  $compiler
    ->raw(', array(');
  foreach ($tokens as $token) {
    $compiler
      ->string($token
      ->getAttribute('placeholder'))
      ->raw(' => ')
      ->subcompile($token)
      ->raw(', ');
  }
  $compiler
    ->raw(')');

  // Write any options passed.
  if ($this
    ->hasNode('options')) {
    $compiler
      ->raw(', ')
      ->subcompile($this
      ->getNode('options'));
  }

  // Write function closure.
  $compiler
    ->raw(')');

  // @todo Add debug output, see https://www.drupal.org/node/2512672
  // End writing.
  $compiler
    ->raw(";\n");
}