You are here

public function CommerceCurrencyResolverAutocomplete::countriesAutocomplete in Commerce Currency Resolver 8

Retrieves group suggestions for a context.

Parameters

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response with groups matching the query.

1 string reference to 'CommerceCurrencyResolverAutocomplete::countriesAutocomplete'
commerce_currency_resolver.routing.yml in ./commerce_currency_resolver.routing.yml
commerce_currency_resolver.routing.yml

File

src/Controller/CommerceCurrencyResolverAutocomplete.php, line 54

Class

CommerceCurrencyResolverAutocomplete
Class CommerceCurrencyResolverAutocomplete.

Namespace

Drupal\commerce_currency_resolver\Controller

Code

public function countriesAutocomplete(Request $request) {
  $query = $request->query
    ->get('q');
  $matches = [];
  foreach ($this->countryManager
    ->getList() as $key => $value) {
    if (stripos($value, $query) === 0) {
      $matches[$key] = $value;
    }
    if (stripos($key, $query) === 0) {
      $matches[$key] = $value;
    }
  }
  $response = [];

  // Format the unique matches to be used with the autocomplete field.
  foreach (array_unique($matches) as $key => $match) {
    $response[] = [
      'value' => $key,
      'label' => Html::escape($match),
    ];
  }
  return new JsonResponse($response);
}