public function CountryAutocompleteController::autocomplete in Country 8
Returns response for the country name autocompletion.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object containing the search string.
string $entity_type: The type of entity that owns the field.
string $bundle: The name of the bundle that owns the field.
$field_name: The name of the field.
Return value
\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions for countries.
1 string reference to 'CountryAutocompleteController::autocomplete'
File
- src/
Controller/ CountryAutocompleteController.php, line 70
Class
- CountryAutocompleteController
- Returns autocomplete responses for countries.
Namespace
Drupal\country\ControllerCode
public function autocomplete(Request $request, $entity_type, $bundle, $field_name) {
$matches = [];
$string = $request->query
->get('q');
if ($string) {
// Check if the bundle is global - in that case we need all countries.
if ($bundle == 'global') {
$countries = $this->countryManager
->getList();
}
else {
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
$field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$countries = $this->countryFieldManager
->getSelectableCountries($field_definition);
}
foreach ($countries as $iso2 => $country) {
if (strpos(mb_strtolower($country), mb_strtolower($string)) !== FALSE) {
$matches[] = [
'value' => $country,
'label' => $country,
];
}
}
}
return new JsonResponse($matches);
}