FormAutocomplete.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // https://www.qed42.com/blog/autocomplete-drupal-8
  3. // https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
  4. namespace Drupal\materio_sapi\Controller;
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Drupal\Component\Utility\Tags;
  9. use Drupal\Component\Utility\Unicode;
  10. use Drupal\search_api\Entity\Index;
  11. /**
  12. * Defines a route controller for entity autocomplete form elements.
  13. */
  14. class FormAutocomplete extends ControllerBase {
  15. /**
  16. * Handler for autocomplete request.
  17. */
  18. public function autocomplete(Request $request) {
  19. // Get the typed string from the URL, if it exists.
  20. if ($input = $request->query->get('q')) {
  21. $typed_string = Tags::explode($input);
  22. $typed_string = Unicode::strtolower(array_pop($typed_string));
  23. // \Drupal::logger('materio_sapi')->notice($typed_string);
  24. $index = Index::load('autocomplete');
  25. $query = $index->query();
  26. // Change the parse mode for the search.
  27. $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
  28. ->createInstance('direct');
  29. $parse_mode->setConjunction('OR');
  30. $query->setParseMode($parse_mode);
  31. // Set fulltext search keywords and fields.
  32. $query->keys($typed_string);
  33. $query->setFulltextFields(['name']);
  34. // Set additional conditions.
  35. // $query->addCondition('status', 1)
  36. // ->addCondition('author', 1, '<>');
  37. // Restrict the search to specific languages.
  38. // $query->setLanguages(['de', 'it']);
  39. // Do paging.
  40. $query->range(0, 10);
  41. // Add sorting.
  42. $query->sort('search_api_relevance', 'DESC');
  43. // Set one or more tags for the query.
  44. // @see hook_search_api_query_TAG_alter()
  45. // @see hook_search_api_results_TAG_alter()
  46. $query->addTag('materio_sapi_autocomplete');
  47. $results = $query->execute();
  48. // $items = $results->getResultItems();
  49. // \Drupal::logger('materio_sapi')->notice($results->getResultCount());
  50. // \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($items)));
  51. $response = [];
  52. foreach ($results as $result) {
  53. // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('tid')->getValues(),true));
  54. // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('name')->getValues(),true));
  55. $tid = $result->getField('tid')->getValues()[0];
  56. $term_name = $result->getField('name')->getValues()[0]->getText();
  57. $response[] = [
  58. 'value' => $tid,
  59. 'label' => $term_name,
  60. ];
  61. }
  62. }
  63. return new JsonResponse($response);
  64. }
  65. }