You are here

public function LingotekSupportedLocalesController::autocomplete in Lingotek Translation 3.2.x

Same name and namespace in other branches
  1. 4.0.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  2. 3.1.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  3. 3.3.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  4. 3.4.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  5. 3.5.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  6. 3.6.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  7. 3.7.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()
  8. 3.8.x src/Controller/LingotekSupportedLocalesController.php \Drupal\lingotek\Controller\LingotekSupportedLocalesController::autocomplete()

Callback for the autocomplete of supported locales.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.

1 string reference to 'LingotekSupportedLocalesController::autocomplete'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekSupportedLocalesController.php, line 66

Class

LingotekSupportedLocalesController

Namespace

Drupal\lingotek\Controller

Code

public function autocomplete(Request $request) {

  // Get autocomplete query.
  $q = $request->query
    ->get('q') ?: '';
  if ($q == '') {
    return new JsonResponse([]);
  }
  $locales = $this->lingotek
    ->getLocalesInfo();
  ksort($locales);
  $matches = [];
  foreach ($locales as $locale => $locale_info) {
    $fieldsToSearch = [
      'code' => $this
        ->t('Code'),
      'language_code' => $this
        ->t('Language Code'),
      // 'title' => $this->t('Title'),
      // 'language' => $this->t('Language'),
      'country_code' => $this
        ->t('Country Code'),
    ];
    foreach ($fieldsToSearch as $field => $fieldDescription) {
      if (stripos($locale_info[$field], $q) !== FALSE) {
        $matches[] = [
          'value' => $locale,
          'label' => new FormattableMarkup('@title (@code) [matched: @match: %value]', [
            '@title' => Html::escape($locale_info['title']),
            '@code' => Html::escape($locale_info['code']),
            '@match' => Html::escape($fieldDescription),
            '%value' => Html::escape($locale_info[$field]),
          ]),
        ];
        continue 2;
      }
    }
  }
  return new JsonResponse($matches);
}