You are here

public function MachineNameController::transliterate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/MachineNameController.php \Drupal\system\MachineNameController::transliterate()

Transliterates a string in given language. Various postprocessing possible.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The input string and language for the transliteration. Optionally may contain the replace_pattern, replace, lowercase parameters.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The transliterated string.

1 string reference to 'MachineNameController::transliterate'
system.routing.yml in core/modules/system/system.routing.yml
core/modules/system/system.routing.yml

File

core/modules/system/src/MachineNameController.php, line 58
Contains \Drupal\system\MachineNameController.

Class

MachineNameController
Controller routines for machine name transliteration routes.

Namespace

Drupal\system

Code

public function transliterate(Request $request) {
  $text = $request->query
    ->get('text');
  $langcode = $request->query
    ->get('langcode');
  $replace_pattern = $request->query
    ->get('replace_pattern');
  $replace = $request->query
    ->get('replace');
  $lowercase = $request->query
    ->get('lowercase');
  $transliterated = $this->transliteration
    ->transliterate($text, $langcode, '_');
  if ($lowercase) {
    $transliterated = Unicode::strtolower($transliterated);
  }
  if (isset($replace_pattern) && isset($replace)) {

    // Quote the pattern delimiter and remove null characters to avoid the e
    // or other modifiers being injected.
    $transliterated = preg_replace('@' . strtr($replace_pattern, [
      '@' => '\\@',
      chr(0) => '',
    ]) . '@', $replace, $transliterated);
  }
  return new JsonResponse($transliterated);
}