class CountryFlagAutocompleteController in Flags 8
Returns autocomplete responses for countries.
Hierarchy
- class \Drupal\country\Controller\CountryAutocompleteController implements ContainerInjectionInterface
- class \Drupal\flags_country\Controller\CountryFlagAutocompleteController
Expanded class hierarchy of CountryFlagAutocompleteController
File
- flags_country/
src/ Controller/ CountryFlagAutocompleteController.php, line 13
Namespace
Drupal\flags_country\ControllerView source
class CountryFlagAutocompleteController extends CountryAutocompleteController {
/**
* Returns response for the country name autocompletion.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object containing the search string.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response containing the autocomplete suggestions for countries.
*/
public function autocomplete(Request $request, $entity_type, $bundle, $field_name) {
/** @var \Drupal\Core\Render\Renderer $renderer */
$renderer = \Drupal::service('renderer');
$matches = array();
$string = $request->query
->get('q');
if ($string) {
$field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$countries = \Drupal::service('country.field.manager')
->getSelectableCountries($field_definition);
foreach ($countries as $iso2 => $country) {
if (strpos(mb_strtolower($country), mb_strtolower($string)) !== FALSE) {
$label = array(
'country' => array(
'#markup' => $country,
),
'flag' => array(
'#theme' => 'flags',
'#code' => strtolower($iso2),
'#source' => 'country',
),
);
$matches[] = array(
'value' => $country,
'label' => $renderer
->render($label),
);
}
}
}
return new JsonResponse($matches);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CountryAutocompleteController:: |
protected | property | The country field manager. | |
CountryAutocompleteController:: |
protected | property | The country manager. | |
CountryAutocompleteController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
CountryAutocompleteController:: |
public | function | Constructs a new CountryAutocompleteController. | |
CountryFlagAutocompleteController:: |
public | function |
Returns response for the country name autocompletion. Overrides CountryAutocompleteController:: |