reinstalled all contribs with composer
need to run : drush ev 'drupal_flush_all_caches();' drush cr and restart nginx and php-fpm before loading the website
This commit is contained in:
@@ -0,0 +1,393 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\edlp_search\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Form\FormBuilder;
|
||||
use Drupal\search_api\Entity\Index;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
// use Drupal\Core\Cache\CacheableJsonResponse;
|
||||
// use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\core\render\RenderContext;
|
||||
|
||||
/**
|
||||
* Class EdlpSearchController.
|
||||
*/
|
||||
class EdlpSearchController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormBuilder
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
protected $sapi_index_id;
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*/
|
||||
public function __construct(FormBuilder $formBuilder) {
|
||||
$this->formBuilder = $formBuilder;
|
||||
$this->sapi_index_id = "collection";
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('form_builder')
|
||||
);
|
||||
}
|
||||
|
||||
private function getSearchForm(){
|
||||
$this->form = $this->formBuilder->getForm('Drupal\edlp_search\Form\EdlpSearchForm');
|
||||
}
|
||||
|
||||
private function buildRenderable(){
|
||||
$this->getSearchForm();
|
||||
$this->renderable = array(
|
||||
"#theme" => "edlp_search_search_form",
|
||||
'#form' => $this->form
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searchform.
|
||||
*
|
||||
* @return string
|
||||
* Return Hello string.
|
||||
*/
|
||||
public function searchForm() {
|
||||
$this->buildRenderable();
|
||||
|
||||
return $this->renderable;
|
||||
}
|
||||
|
||||
/**
|
||||
* searchFormJson
|
||||
*/
|
||||
public function searchFormJson(){
|
||||
$this->buildRenderable();
|
||||
|
||||
$rendered = render($this->renderable);
|
||||
$data = [
|
||||
'rendered' => $rendered,
|
||||
'title' => 'Search',
|
||||
];
|
||||
|
||||
// translations links
|
||||
$route_name = 'edlp_search.edlp_search_controller_searchForm';
|
||||
$links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
|
||||
if (isset($links->links)) {
|
||||
$translations_build = [
|
||||
'#theme' => 'links__language_block',
|
||||
'#links' => $links->links,
|
||||
'#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
|
||||
'#set_active_class' => TRUE,
|
||||
];
|
||||
$translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
|
||||
|
||||
$data['translations_links'] = $translations_rendered;
|
||||
}
|
||||
|
||||
// TODO: make respponse cachable
|
||||
$response = new JsonResponse();
|
||||
$response->setData($data);
|
||||
return $response;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
$query->setLanguages(array($current_langcode));
|
||||
|
||||
// $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);
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
// dpm($items);
|
||||
$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;
|
||||
}
|
||||
|
||||
// dpm($fields);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
// dpm($this->matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
// }
|
||||
// if($this->request->query->get('node_type')){
|
||||
// $this->node_type = $this->request->query->get('node_type');
|
||||
// }
|
||||
$this->keys = $this->request->query->get('q');
|
||||
}
|
||||
|
||||
/**
|
||||
* searchResults
|
||||
*/
|
||||
public function searchResults(Request $request) {
|
||||
|
||||
$this->request = $request;
|
||||
|
||||
$this->getRequestSearchArgs();
|
||||
|
||||
return $this->getrenderable();
|
||||
}
|
||||
|
||||
/**
|
||||
* searchResultsJson
|
||||
*/
|
||||
public function searchResultsJson(Request $request){
|
||||
|
||||
$this->request = $request;
|
||||
|
||||
$this->getRequestSearchArgs();
|
||||
|
||||
$renderable = $this->getrenderable();
|
||||
$rendered = render($renderable);
|
||||
|
||||
// build an array of results's nids
|
||||
$results_nids = [];
|
||||
foreach ($this->items as $item) {
|
||||
$results_nids[] = $item->id();
|
||||
}
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setData([
|
||||
'test'=>'search results',
|
||||
'keys'=>$this->keys,
|
||||
'entries' => $this->entries,
|
||||
'entry_names' => $this->entry_names,
|
||||
'langues' => $this->langues,
|
||||
'genres' => $this->genres,
|
||||
'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(){
|
||||
|
||||
// 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();
|
||||
// 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);
|
||||
$keys_condition_group = $query->createConditionGroup('OR');
|
||||
$keys_condition_group->addCondition('title', $this->keys, 'IN');
|
||||
$keys_condition_group->addCondition('field_description', $this->keys, 'IN');
|
||||
$query->addConditionGroup($keys_condition_group);
|
||||
}
|
||||
|
||||
// langues
|
||||
if (!empty($this->langues)) {
|
||||
$query->addCondition('field_langues', $this->langues, "=");
|
||||
}
|
||||
|
||||
// genres
|
||||
if (!empty($this->genres)) {
|
||||
$query->addCondition('field_genres', $this->genres, "=");
|
||||
}
|
||||
|
||||
// entries
|
||||
$this->entry_names = [];
|
||||
if (!empty($this->entries)){
|
||||
$terms = entity_load_multiple('taxonomy_term', $this->entries);
|
||||
$entries_condition_group = $query->createConditionGroup();
|
||||
foreach ($terms as $tid => $term) {
|
||||
$entries_condition_group->addCondition('field_entrees', (int)$tid, "=");
|
||||
$this->entry_names[] = $term->getName();
|
||||
}
|
||||
// dpm($entries_condition_group);
|
||||
$query->addConditionGroup($entries_condition_group);
|
||||
}
|
||||
|
||||
// TODO: locuteurs
|
||||
|
||||
$current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
$query->setLanguages(array($current_langcode));
|
||||
|
||||
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
// // $language = \Drupal::languageManager()->getCurrentLanguage()->getName();
|
||||
// $query->addCondition('search_api_language', $language);
|
||||
|
||||
$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 getRequestSearchArgs(){
|
||||
// dpm($this->request);
|
||||
$this->keys = $this->request->query->get('keys');
|
||||
$this->langues = $this->request->query->get('langues');
|
||||
$this->genres = $this->request->query->get('genres');
|
||||
$this->entries = $this->request->query->get('entries');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user