#1906 serach form autocmplete multi terms

This commit is contained in:
2022-08-12 17:00:49 +02:00
parent 75ea440f89
commit d1b4902aa3
11 changed files with 96 additions and 55 deletions

View File

@@ -57,11 +57,14 @@ class Base extends ControllerBase {
}
// in case of term id provided restrict the keys to taxo fields
if ($this->term) {
if ($this->terms && count($this->terms)) {
$term_conditions = $this->and_query->createConditionGroup('OR');
$term = (int) $this->term;
foreach (['tag_tid', 'thesaurus_tid'] as $field) {
$term_conditions->addCondition($field, $term);
// $term = (int) $this->term;
foreach ($this->terms as $term) {
$tid = $term->value;
foreach (['tag_tid', 'thesaurus_tid'] as $field) {
$term_conditions->addCondition($field, (int) $tid);
}
}
$this->and_query->addConditionGroup($term_conditions);
@@ -265,7 +268,11 @@ class Base extends ControllerBase {
\Drupal::logger('materio_sapi')->notice($this->keys);
}
// get the exacte term id in case of autocomplete
$this->term = $request->query->get('term');
// $this->terms = $request->query->get('terms');
$t = $request->query->get('terms');
// $this->terms = strlen($t) ? explode(',', $t) : null;
$this->terms = strlen($t) ? json_decode($t) : null;
// get the filters of advanced search
$f = $request->query->get('filters');
$this->filters = strlen($f) ? explode(',', $f) : null;
@@ -295,7 +302,7 @@ class Base extends ControllerBase {
$this->sapiQuery();
$resp['keys'] = $this->keys;
$resp['term'] = $this->term;
$resp['terms'] = json_encode($this->terms);
$resp['filters'] = $this->filters;
// $resp['count'] = $this->results->getResultCount();
$resp['count'] = count($this->results['nids']);

View File

@@ -23,10 +23,9 @@ class FormAutocomplete extends ControllerBase {
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));
$typed_string = mb_strtolower(array_pop($typed_string));
// \Drupal::logger('materio_sapi')->notice($typed_string);
$tag_list = Tags::explode($input); // does not work with space separated words
// $tag_list = explode(" ", $input);
$typed_string = !empty($tag_list) ? mb_strtolower(array_pop($tag_list)) : '';
$index = Index::load('autocomplete');
$query = $index->query();
@@ -74,6 +73,7 @@ class FormAutocomplete extends ControllerBase {
$tid = $result->getField('tid')->getValues()[0];
$term_name = $result->getField('name')->getValues()[0]->getText();
$response[] = [
// 'typed_string' => $typed_string,
'value' => $tid,
'label' => $term_name,
];