You are here

public function EntityAutocomplete::autocomplete in Synonyms 8

Routing callback: provide suggestions for autocomplete form element.

1 string reference to 'EntityAutocomplete::autocomplete'
synonyms.routing.yml in ./synonyms.routing.yml
synonyms.routing.yml

File

src/Controller/EntityAutocomplete.php, line 45

Class

EntityAutocomplete
Synonyms friendly entity autocomplete controller.

Namespace

Drupal\synonyms\Controller

Code

public function autocomplete(Request $request, EntityTypeInterface $target_type, $token) {
  $matches = [];
  if ($input = $request
    ->get('q')) {
    $input = Tags::explode($input);
    $input = array_pop($input);
    foreach ($this->autocompleteService
      ->autocompleteLookup($input, $token) as $k => $suggestion) {
      $key = $suggestion['entity_label'] . ' (' . $suggestion['entity_id'] . ')';
      $key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));

      // Names containing commas or quotes must be wrapped in quotes.
      $key = Tags::encode($key);
      $matches[] = [
        'value' => $key,
        'label' => $suggestion['wording'],
      ];
    }
  }
  return new JsonResponse($matches);
}