FormAutocomplete.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $typed_string = mb_strtolower(array_pop($typed_string));
  24. // \Drupal::logger('materio_sapi')->notice($typed_string);
  25. $index = Index::load('autocomplete');
  26. $query = $index->query();
  27. // Change the parse mode for the search.
  28. $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
  29. ->createInstance('direct');
  30. $parse_mode->setConjunction('OR');
  31. $query->setParseMode($parse_mode);
  32. // Set fulltext search keywords and fields.
  33. $query->keys($typed_string);
  34. $query->setFulltextFields(['name', 'field_synonyms']);
  35. // Set additional conditions.
  36. $query->addCondition('status', 1);
  37. // ->addCondition('author', 1, '<>');
  38. // Restrict the search to specific languages.
  39. $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
  40. $query->setLanguages([$lang]);
  41. // Do paging.
  42. $query->range(0, 15);
  43. // Add sorting.
  44. $query->sort('search_api_relevance', 'DESC');
  45. // Set one or more tags for the query.
  46. // @see hook_search_api_query_TAG_alter()
  47. // @see hook_search_api_results_TAG_alter()
  48. $query->addTag('materio_sapi_autocomplete');
  49. $results = $query->execute();
  50. \Drupal::logger('materio_sapi')->notice($results->getResultCount());
  51. // $items = $results->getResultItems();
  52. // \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($items)));
  53. $response = [];
  54. foreach ($results as $result) {
  55. // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('tid')->getValues(),true));
  56. // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('name')->getValues(),true));
  57. // \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($result)));
  58. $tid = $result->getField('tid')->getValues()[0];
  59. $term_name = $result->getField('name')->getValues()[0]->getText();
  60. $response[] = [
  61. 'value' => $tid,
  62. 'label' => $term_name,
  63. ];
  64. }
  65. }
  66. return new JsonResponse($response);
  67. }
  68. }