draft of materio_sapi autocomplete
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
materio_sapi.search_autocomplete:
|
||||
path: '/materio_sapi/form/autocomplete'
|
||||
defaults:
|
||||
_controller: '\Drupal\materio_sapi\Controller\FormAutocomplete::Autocomplete'
|
||||
_format: json
|
||||
requirements:
|
||||
_permission: 'access materio search'
|
||||
#
|
||||
# materio_sapi.materio_sapi_search_form:
|
||||
# path: '/materio_sapi/form/materio_sapi_search'
|
||||
|
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
// https://www.qed42.com/blog/autocomplete-drupal-8
|
||||
// https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
|
||||
|
||||
namespace Drupal\materio_sapi\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Drupal\Component\Utility\Tags;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\search_api\Entity\Index;
|
||||
|
||||
|
||||
/**
|
||||
* Defines a route controller for entity autocomplete form elements.
|
||||
*/
|
||||
class FormAutocomplete extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Handler for autocomplete request.
|
||||
*/
|
||||
public function autocomplete(Request $request) {
|
||||
// Get the typed string from the URL, if it exists.
|
||||
if ($input = $request->query->get('q')) {
|
||||
$typed_string = Tags::explode($input);
|
||||
$typed_string = Unicode::strtolower(array_pop($typed_string));
|
||||
// \Drupal::logger('materio_sapi')->notice($typed_string);
|
||||
|
||||
$index = Index::load('autocomplete');
|
||||
$query = $index->query();
|
||||
|
||||
// Change the parse mode for the search.
|
||||
$parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
|
||||
->createInstance('direct');
|
||||
$parse_mode->setConjunction('OR');
|
||||
$query->setParseMode($parse_mode);
|
||||
|
||||
// Set fulltext search keywords and fields.
|
||||
$query->keys($typed_string);
|
||||
$query->setFulltextFields(['name']);
|
||||
|
||||
// Set additional conditions.
|
||||
// $query->addCondition('status', 1)
|
||||
// ->addCondition('author', 1, '<>');
|
||||
|
||||
// Restrict the search to specific languages.
|
||||
// $query->setLanguages(['de', 'it']);
|
||||
|
||||
// Do paging.
|
||||
$query->range(0, 10);
|
||||
|
||||
// Add sorting.
|
||||
$query->sort('search_api_relevance', 'DESC');
|
||||
|
||||
// Set one or more tags for the query.
|
||||
// @see hook_search_api_query_TAG_alter()
|
||||
// @see hook_search_api_results_TAG_alter()
|
||||
$query->addTag('materio_sapi_autocomplete');
|
||||
|
||||
// Execute the search.
|
||||
$results = $query->execute();
|
||||
|
||||
// echo "Result count: {$results->getResultCount()}\n";
|
||||
// $ids = implode(', ', array_keys($results->getResultItems()));
|
||||
// echo "Returned IDs: $ids.\n";
|
||||
|
||||
// $items = $results->getResultItems();
|
||||
// \Drupal::logger('materio_sapi')->notice($results->getResultCount());
|
||||
// \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($items)));
|
||||
|
||||
$response = [];
|
||||
foreach ($results as $result) {
|
||||
// \Drupal::logger('materio_sapi')->notice(print_r($result->getField('tid')->getValues(),true));
|
||||
// \Drupal::logger('materio_sapi')->notice(print_r($result->getField('name')->getValues(),true));
|
||||
$tid = $result->getField('tid')->getValues()[0];
|
||||
$term_name = $result->getField('name')->getValues()[0]->getText();
|
||||
$response[] = [
|
||||
'value' => $tid,
|
||||
'label' => $term_name,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonResponse($response);
|
||||
}
|
||||
|
||||
}
|
@@ -24,14 +24,18 @@ class MaterioSapiSearchForm extends FormBase {
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['search'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Search'),
|
||||
// '#title' => $this->t('Search'),
|
||||
'#maxlength' => 64,
|
||||
'#size' => 64,
|
||||
'#size' => 25,
|
||||
'#weight' => '0',
|
||||
'#attributes' => [
|
||||
"placeholder" => $this->t('Search'),
|
||||
],
|
||||
'#autocomplete_route_name' => 'materio_sapi.search_autocomplete',
|
||||
];
|
||||
$form['submit'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Submit'),
|
||||
'#value' => $this->t('Search'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
|
Reference in New Issue
Block a user