autocomplete for genres and langues search form fields is working

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-19 17:23:34 +01:00
parent a7f736f919
commit 8f7ab6bb66
3 changed files with 130 additions and 7 deletions
@@ -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:
@@ -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');
}
@@ -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'] = [