diff --git a/sites/all/modules/figli/edlp_search/edlp_search.routing.yml b/sites/all/modules/figli/edlp_search/edlp_search.routing.yml index 7199e93fb..3552bbfa3 100644 --- a/sites/all/modules/figli/edlp_search/edlp_search.routing.yml +++ b/sites/all/modules/figli/edlp_search/edlp_search.routing.yml @@ -15,6 +15,15 @@ edlp_search.edlp_search_controller_searchForm_ajax: requirements: _permission: 'access content' +edlp_search.edlp_search_controller_autocomplete: + path: '/edlp_search/searchform/autocomplete' + defaults: + _controller: '\Drupal\edlp_search\Controller\EdlpSearchController::autocomplete' + _title: 'Autocomplete' + requirements: + _permission: 'access content' + + edlp_search.edlp_search_controller_search_results: path: '/edlp_search/searchresults' defaults: diff --git a/sites/all/modules/figli/edlp_search/src/Controller/EdlpSearchController.php b/sites/all/modules/figli/edlp_search/src/Controller/EdlpSearchController.php index c4e40804e..a2647f503 100644 --- a/sites/all/modules/figli/edlp_search/src/Controller/EdlpSearchController.php +++ b/sites/all/modules/figli/edlp_search/src/Controller/EdlpSearchController.php @@ -21,11 +21,14 @@ class EdlpSearchController extends ControllerBase { */ protected $formBuilder; + protected $sapi_index_id; + /** * Construct. */ public function __construct(FormBuilder $formBuilder) { $this->formBuilder = $formBuilder; + $this->sapi_index_id = "collection"; } /** @@ -78,6 +81,98 @@ class EdlpSearchController extends ControllerBase { } + public function autocomplete(Request $request){ + // TODO: get field_name parameter + $this->request = $request; + $this->getRequestAutocompleteArgs(); + + // run a search on selected field and get the fields unique values + $this->autocompleteQuery(); + + $response = new JsonResponse($this->matches); + return $response; + + // return array( + // "#markup" => "autocomplete" + // ); + } + + /** + * autocompleteQuery + */ + private function autocompleteQuery(){ + + // TODO: get the sapi index id with settings + /* @var $sapi_index \Drupal\search_api\IndexInterface */ + $sapi_index = Index::load($this->sapi_index_id); + // dpm($sapi_index); + + // Create the query. + $query = $sapi_index->query(); + + $query->setSearchID('edlp_search:autocomplete'); + + $parse_mode = \Drupal::getContainer() + ->get('plugin.manager.search_api.parse_mode') + ->createInstance('direct'); + $query->setParseMode($parse_mode); + + // $query->range(0, 20); + + // workflow + $wf_condition_group = $query->createConditionGroup('OR'); + $wf_condition_group->addCondition('field_workflow', 'corpus_documents_publie', "="); + // TODO: add condition with the other workflow field + $query->addConditionGroup($wf_condition_group); + + // Search for keys. + if (!empty($this->keys)) { + // dpm($this->keys); + $query->keys($this->keys); + } + + // select the field in which search will be performed + $query->setFulltextFields(array($this->field_name)); + + $result = $query->execute(); + $items = $result->getResultItems(); + + $this->matches = []; + foreach ($items as $item) { + try { + /** @var \Drupal\Core\Entity\EntityInterface $entity */ + $fields = $item->getField($this->field_name)->getValues(); + }catch (SearchApiException $e) { + continue; + } + if (!$fields) { + continue; + } + + $field_text = $fields[0]->getText(); + + if( in_array($field_text, $this->matches) ){ + continue; + } + + $this->matches[] = $field_text; + + if(count($this->matches) > 14){ + break; + } + } + // dpm($this->matches); + } + + /** + * getRequestArgs + */ + private function getRequestAutocompleteArgs(){ + // dpm($this->request); + $this->field_name = $this->request->query->get('field_name'); + $this->keys = $this->request->query->get('q'); + } + /** * searchResults */ @@ -85,7 +180,7 @@ class EdlpSearchController extends ControllerBase { $this->request = $request; - // TODO: what if no results + $this->getRequestSearchArgs(); return $this->getrenderable(); } @@ -97,6 +192,8 @@ class EdlpSearchController extends ControllerBase { $this->request = $request; + $this->getRequestSearchArgs(); + $renderable = $this->getrenderable(); $rendered = render($renderable); @@ -106,8 +203,6 @@ class EdlpSearchController extends ControllerBase { $results_nids[] = $item->id(); } - // TODO: what if no results - $response = new JsonResponse(); $response->setData([ 'test'=>'search results', @@ -151,11 +246,9 @@ class EdlpSearchController extends ControllerBase { */ private function query(){ - $this->getRequestArgs(); - // TODO: get the sapi index id with settings /* @var $sapi_index \Drupal\search_api\IndexInterface */ - $sapi_index = Index::load('collection'); + $sapi_index = Index::load($this->sapi_index_id); // dpm($sapi_index); // Create the query. @@ -222,7 +315,8 @@ class EdlpSearchController extends ControllerBase { /** * getRequestArgs */ - private function getRequestArgs(){ + private function getRequestSearchArgs(){ + // dpm($this->request); $this->keys = $this->request->query->get('keys'); $this->entries = $this->request->query->get('entries'); } diff --git a/sites/all/modules/figli/edlp_search/src/Form/EdlpSearchForm.php b/sites/all/modules/figli/edlp_search/src/Form/EdlpSearchForm.php index 630bffb30..51e44008a 100644 --- a/sites/all/modules/figli/edlp_search/src/Form/EdlpSearchForm.php +++ b/sites/all/modules/figli/edlp_search/src/Form/EdlpSearchForm.php @@ -32,6 +32,26 @@ class EdlpSearchForm extends FormBase { '#weight' => '0', ]; + $form['language'] = [ + '#type' => 'textfield', + '#title' => $this->t('Language'), + '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete', + '#autocomplete_route_parameters' => array('field_name' => 'field_langues'), + '#maxlength' => 64, + '#size' => 15, + '#weight' => '0', + ]; + + $form['genres'] = [ + '#type' => 'textfield', + '#title' => $this->t('Genres'), + '#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete', + '#autocomplete_route_parameters' => array('field_name' => 'field_genres'), + '#maxlength' => 64, + '#size' => 15, + '#weight' => '0', + ]; + $this->getEntries(); $form['entries'] = [