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'
File
- src/
Controller/ CommerceCurrencyResolverAutocomplete.php, line 54
Class
- CommerceCurrencyResolverAutocomplete
- Class CommerceCurrencyResolverAutocomplete.
Namespace
Drupal\commerce_currency_resolver\ControllerCode
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);
}