added synonyms to search autocomplete and results, improved collection admin view

This commit is contained in:
Bachir Soussi Chiadmi
2018-04-11 17:55:57 +02:00
parent 07a138cfe6
commit 1ae73c1c12
5 changed files with 142 additions and 3 deletions
@@ -93,6 +93,8 @@ class EdlpSearchController extends ControllerBase {
$response = new JsonResponse($this->matches);
return $response;
// dpm($this->matches, 'matches');
//
// return array(
// "#markup" => "autocomplete"
// );
@@ -132,32 +134,45 @@ class EdlpSearchController extends ControllerBase {
$query->keys($this->keys);
}
// select the field in which search will be performed
$query->setFulltextFields(array($this->field_name));
if(null !== $this->field_synonyms){
// select the taxo field AND the synonym field in which search will be performed
$query->setFulltextFields(array($this->field_name, $this->field_synonyms));
}else{
// OR select the unique taxo 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) {
// get the field from item
try {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$fields = $item->getField($this->field_name)->getValues();
}catch (SearchApiException $e) {
// if error on getinfg the field, skip item
continue;
}
// if no fields returned, skip item
if (!$fields) {
continue;
}
// get field content
$field_text = $fields[0]->getText();
// if content already in matches, skip item
if( in_array($field_text, $this->matches) ){
continue;
}
// add the item to the return list
$this->matches[] = $field_text;
// do not return more than 14 items
if(count($this->matches) > 14){
break;
}
@@ -169,8 +184,12 @@ class EdlpSearchController extends ControllerBase {
* getRequestArgs
*/
private function getRequestAutocompleteArgs(){
// args are provided by form definition with autocomplete_route_parameters
// dpm($this->request);
$this->field_name = $this->request->query->get('field_name');
// if($this->request->query->get('field_synonyms')){
$this->field_synonyms = $this->request->query->get('field_synonyms');
// }
$this->keys = $this->request->query->get('q');
}
@@ -54,7 +54,7 @@ class EdlpSearchForm extends FormBase {
'genres' => [
'#type' => 'textfield',
'#autocomplete_route_name' => 'edlp_search.edlp_search_controller_autocomplete',
'#autocomplete_route_parameters' => array('field_name' => 'field_genres'),
'#autocomplete_route_parameters' => array('field_name' => 'field_genres', 'field_synonyms' => 'field_synonyms'),
'#maxlength' => 20,
'#size' => 15,
'#weight' => '0',