search is ok! (remains to sync search and corpus map
This commit is contained in:
@@ -4,9 +4,9 @@ namespace Drupal\edlp_search\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Drupal\Core\Form\FormBuilder;
|
||||
use Drupal\search_api\Entity\Index;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
@@ -62,6 +62,9 @@ class EdlpSearchController extends ControllerBase {
|
||||
return $this->renderable;
|
||||
}
|
||||
|
||||
/**
|
||||
* searchFormJson
|
||||
*/
|
||||
public function searchFormJson(){
|
||||
$this->buildRenderable();
|
||||
|
||||
@@ -76,27 +79,152 @@ class EdlpSearchController extends ControllerBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Searchresults.
|
||||
*
|
||||
* @return string
|
||||
* Return Hello string.
|
||||
*/
|
||||
public function searchResults() {
|
||||
return [
|
||||
'#type' => 'markup',
|
||||
'#markup' => $this->t('Implement method: searchResults')
|
||||
];
|
||||
* searchResults
|
||||
*/
|
||||
public function searchResults(Request $request) {
|
||||
|
||||
$this->request = $request;
|
||||
|
||||
// TODO: what if no results
|
||||
|
||||
return $this->getrenderable();
|
||||
}
|
||||
|
||||
/**
|
||||
* searchResultsJson
|
||||
*/
|
||||
public function searchResultsJson(Request $request){
|
||||
|
||||
$keys = $request->query->get('keys');
|
||||
$this->request = $request;
|
||||
|
||||
$renderable = $this->getrenderable();
|
||||
$rendered = render($renderable);
|
||||
|
||||
// build an array of results's nids
|
||||
$results_nids = [];
|
||||
foreach ($this->items as $item) {
|
||||
$results_nids[] = $item->id();
|
||||
}
|
||||
|
||||
// TODO: what if no results
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setData([
|
||||
'test'=>'search results',
|
||||
'keys'=>$keys
|
||||
// 'rendered'=> $rendered,
|
||||
'keys'=>$this->keys,
|
||||
'entries' => $this->entries,
|
||||
'rendered'=> $rendered,
|
||||
'results_nids'=>$results_nids,
|
||||
]);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* getRenderable
|
||||
*/
|
||||
private function getRenderable(){
|
||||
$this->query();
|
||||
|
||||
$build = array(
|
||||
'#theme' => 'edlp_search_results',
|
||||
'#items' => $this->items
|
||||
);
|
||||
if(!count($this->items)){
|
||||
$build['#no_results_found'] = array(
|
||||
'#markup' => t('Your search yielded no results.')
|
||||
);
|
||||
// $build['#search_help'] = array(
|
||||
// '#markup' => $this->t('<ul>
|
||||
// <li>Check if your spelling is correct.</li>
|
||||
// <li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>"bike shed"</em>.</li>
|
||||
// <li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
|
||||
// </ul>')
|
||||
// );
|
||||
}
|
||||
|
||||
return $build;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* query
|
||||
*/
|
||||
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');
|
||||
// dpm($sapi_index);
|
||||
|
||||
// Create the query.
|
||||
$query = $sapi_index->query();
|
||||
// dpm($query);
|
||||
|
||||
$query->setSearchID('edlp_search:ajaxsearch');
|
||||
|
||||
$parse_mode = \Drupal::getContainer()
|
||||
->get('plugin.manager.search_api.parse_mode')
|
||||
->createInstance('direct');
|
||||
$query->setParseMode($parse_mode);
|
||||
|
||||
// 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)) {
|
||||
$query->keys($this->keys);
|
||||
}
|
||||
|
||||
// entries
|
||||
if (!empty($this->entries)){
|
||||
$entries_condition_group = $query->createConditionGroup();
|
||||
foreach ($this->entries as $tid) {
|
||||
$entries_condition_group->addCondition('field_entrees', (int)$tid, "=");
|
||||
}
|
||||
// dpm($entries_condition_group);
|
||||
$query->addConditionGroup($entries_condition_group);
|
||||
}
|
||||
|
||||
|
||||
// TODO: genres
|
||||
|
||||
// TODO: locuteurs
|
||||
|
||||
// TODO: langues
|
||||
|
||||
$result = $query->execute();
|
||||
$items = $result->getResultItems();
|
||||
// dpm($items);
|
||||
|
||||
$this->items = [];
|
||||
foreach ($items as $item) {
|
||||
try {
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity */
|
||||
$entity = $item->getOriginalObject()->getValue();
|
||||
}
|
||||
catch (SearchApiException $e) {
|
||||
continue;
|
||||
}
|
||||
if (!$entity) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->items[] = $entity;
|
||||
}
|
||||
// dpm($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* getRequestArgs
|
||||
*/
|
||||
private function getRequestArgs(){
|
||||
$this->keys = $this->request->query->get('keys');
|
||||
$this->entries = $this->request->query->get('entries');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user