You are here

public function IpGeoLocController::regionAutocomplete in IP Geolocation Views & Maps 8

Autocompletes the partial region received.

1 string reference to 'IpGeoLocController::regionAutocomplete'
ip_geoloc.routing.yml in ./ip_geoloc.routing.yml
ip_geoloc.routing.yml

File

src/Controller/IpGeoLocController.php, line 99

Class

IpGeoLocController
Pending doc.

Namespace

Drupal\ip_geoloc\Controller

Code

public function regionAutocomplete(Request $request) {
  $partial_region = $request->query
    ->get('q');
  $matches = [];
  if (strlen($partial_region) >= 2) {
    $geo_vocabulary_id = \Drupal::state()
      ->get('ip_geoloc_geo_vocabulary_id', 0);
    foreach (taxonomy_get_tree($geo_vocabulary_id) as $term) {
      $term_name = SafeMarkup::checkPlain($term->name);

      // We define a "match" as any 2 consecutive chars, case-insensitive.
      $is_match = stripos($term_name, $partial_region) !== FALSE;
      if ($is_match) {
        $matches[$term_name] = $term_name;
      }
    }
  }
  return new JsonResponse($matches);
}