function DrupalTranslator::processParameters

Same name in other branches
  1. 9 core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()
  2. 8.9.x core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()
  3. 10 core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()

Processes the parameters array for use with TranslatableMarkup.

2 calls to DrupalTranslator::processParameters()
DrupalTranslator::trans in core/lib/Drupal/Core/Validation/DrupalTranslator.php
DrupalTranslator::transChoice in core/lib/Drupal/Core/Validation/DrupalTranslator.php

File

core/lib/Drupal/Core/Validation/DrupalTranslator.php, line 75

Class

DrupalTranslator
Translates strings using Drupal's translation system.

Namespace

Drupal\Core\Validation

Code

protected function processParameters(array $parameters) {
    $return = [];
    foreach ($parameters as $key => $value) {
        // We allow the values in the parameters to be safe string objects. This
        // can be useful when we want to use parameter values that are
        // TranslatableMarkup.
        if ($value instanceof MarkupInterface) {
            $value = (string) $value;
        }
        if (is_object($value)) {
            // TranslatableMarkup does not work with objects being passed as
            // replacement strings.
        }
        elseif (str_starts_with($key, '{{ ') && strrpos($key, ' }}') == strlen($key) - 3) {
            // Transform it into a Drupal pattern using the format %name.
            $key = '%' . substr($key, 3, strlen($key) - 6);
            $return[$key] = $value;
        }
        else {
            $return[$key] = $value;
        }
    }
    return $return;
}

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