2019-06-01 16:15:08 +02:00
|
|
|
<?php
|
|
|
|
// https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
|
|
|
|
|
|
|
|
namespace Drupal\materio_sapi\Controller;
|
|
|
|
|
|
|
|
use Drupal\Core\Controller\ControllerBase;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Drupal\Component\Utility\Tags;
|
|
|
|
use Drupal\Component\Utility\Unicode;
|
|
|
|
use Drupal\search_api\Entity\Index;
|
|
|
|
|
|
|
|
// https://drupal.stackexchange.com/questions/225008/programatically-use-search-api
|
|
|
|
|
|
|
|
/**
|
2019-06-04 22:38:44 +02:00
|
|
|
* Defines a route controller for materio sapi search (regular and ajax).
|
2019-06-01 16:15:08 +02:00
|
|
|
*/
|
|
|
|
class Base extends ControllerBase {
|
|
|
|
|
2021-01-19 16:30:14 +01:00
|
|
|
|
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
private $limit = 15;
|
|
|
|
private $offset = 0;
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
private function sapiQuery(){
|
|
|
|
$this->index = Index::load('database');
|
|
|
|
$this->query = $this->index->query();
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Change the parse mode for the search.
|
|
|
|
$parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
|
|
|
|
->createInstance('direct');
|
|
|
|
$parse_mode->setConjunction('OR');
|
|
|
|
$this->query->setParseMode($parse_mode);
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Set fulltext search keywords and fields.
|
|
|
|
$this->query->keys($this->keys);
|
|
|
|
// $this->query->setFulltextFields(['name']);
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Set additional conditions.
|
|
|
|
// $this->query->addCondition('status', 1)
|
|
|
|
// ->addCondition('author', 1, '<>');
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Restrict the search to specific languages.
|
|
|
|
// $this->query->setLanguages(['de', 'it']);
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Do paging.
|
|
|
|
$this->query->range($this->offset, $this->limit);
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Add sorting.
|
|
|
|
$this->query->sort('search_api_relevance', 'DESC');
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
// Set one or more tags for the query.
|
|
|
|
// @see hook_search_api_query_TAG_alter()
|
|
|
|
// @see hook_search_api_results_TAG_alter()
|
|
|
|
$this->query->addTag('materio_sapi_search');
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
$this->results = $this->query->execute();
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-01-19 16:30:14 +01:00
|
|
|
private function defaultQuery(){
|
|
|
|
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
|
|
|
|
$this->query = $entity_storage->getQuery()
|
|
|
|
->condition('type', 'materiau')
|
2021-02-22 16:05:54 +01:00
|
|
|
->condition('status', '1')
|
2021-01-19 16:30:14 +01:00
|
|
|
->range($this->offset, $this->limit)
|
|
|
|
->accessCheck(TRUE)
|
|
|
|
->sort('changed', 'DESC');
|
|
|
|
// ->condition('field_example', 'test_value')
|
|
|
|
$this->results = $this->query->execute();
|
|
|
|
|
|
|
|
$this->count_query = $entity_storage->getQuery()
|
|
|
|
->condition('type', 'materiau')
|
|
|
|
->accessCheck(TRUE)
|
2021-02-22 16:05:54 +01:00
|
|
|
->condition('status', '1')
|
2021-01-19 16:30:14 +01:00
|
|
|
->count();
|
|
|
|
$this->count = $this->count_query->execute();
|
|
|
|
}
|
2019-06-04 22:38:44 +02:00
|
|
|
/**
|
|
|
|
* get params from request
|
|
|
|
*/
|
|
|
|
private function parseRequest(Request $request){
|
|
|
|
// Get the typed string from the URL, if it exists.
|
|
|
|
$this->keys = $request->query->get('keys');
|
|
|
|
if($this->keys){
|
|
|
|
$this->keys = Unicode::strtolower($this->keys);
|
|
|
|
// $this->keys = Tags::explode($this->keys);
|
|
|
|
\Drupal::logger('materio_sapi')->notice($this->keys);
|
|
|
|
}
|
|
|
|
$this->term = $request->query->get('term');
|
|
|
|
$this->offset = $request->query->get('offset') ?? $this->offset;
|
|
|
|
$this->limit = $request->query->get('limit') ?? $this->limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for ajax search.
|
|
|
|
*/
|
|
|
|
public function getResults(Request $request) {
|
|
|
|
$this->parseRequest($request);
|
|
|
|
|
|
|
|
$resp = [
|
|
|
|
'range' => array(
|
|
|
|
'offset' => $this->offset,
|
|
|
|
'limit' => $this->limit
|
|
|
|
),
|
|
|
|
];
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
if ($this->keys) {
|
|
|
|
$this->sapiQuery();
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2021-01-19 16:30:14 +01:00
|
|
|
$resp['keys'] = $this->keys;
|
|
|
|
$resp['term'] = $this->term;
|
2019-06-04 22:38:44 +02:00
|
|
|
$resp['count'] = $this->results->getResultCount();
|
2019-06-06 18:12:07 +02:00
|
|
|
$resp['infos'] = t('The search found @count result(s) with keywords @keys.', array(
|
|
|
|
"@count" => $resp['count'],
|
|
|
|
"@keys" => $this->keys
|
|
|
|
));
|
2020-02-18 15:53:21 +01:00
|
|
|
|
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
$resp['options'] = $this->query->getOptions();
|
2019-06-01 16:15:08 +02:00
|
|
|
|
2019-06-11 13:19:27 +02:00
|
|
|
// $items = [];
|
|
|
|
$uuids = [];
|
2020-11-24 14:07:10 +01:00
|
|
|
$nids = [];
|
2019-06-04 22:38:44 +02:00
|
|
|
foreach ($this->results as $result) {
|
2019-06-11 13:19:27 +02:00
|
|
|
// $nid = $result->getField('nid')->getValues()[0];
|
|
|
|
// $uuid = $result->getField('uuid')->getValues()[0];
|
|
|
|
// $title = $result->getField('title')->getValues()[0]->getText();
|
|
|
|
// $items[] = [
|
|
|
|
// 'nid' => $nid,
|
|
|
|
// 'uuid' => $uuid,
|
|
|
|
// 'title' => $title,
|
|
|
|
// ];
|
|
|
|
$uuids[] = $result->getField('uuid')->getValues()[0];
|
2020-11-24 14:07:10 +01:00
|
|
|
$nids[] = $result->getField('nid')->getValues()[0];
|
2019-06-01 16:15:08 +02:00
|
|
|
}
|
2019-06-11 13:19:27 +02:00
|
|
|
// $resp['items'] = $items;
|
|
|
|
$resp['uuids'] = $uuids;
|
2020-11-24 14:07:10 +01:00
|
|
|
$resp['nids'] = $nids;
|
2021-01-19 16:30:14 +01:00
|
|
|
} else {
|
|
|
|
// no keys or terms to search for
|
|
|
|
// display the default base page
|
|
|
|
$this->defaultQuery();
|
|
|
|
// $uuids = [];
|
|
|
|
$nids = [];
|
2021-02-22 16:05:54 +01:00
|
|
|
// Using entityTypeManager
|
|
|
|
// Get a node storage object.
|
|
|
|
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
|
|
|
|
|
2021-01-19 16:30:14 +01:00
|
|
|
foreach ($this->results as $result) {
|
2021-02-22 16:05:54 +01:00
|
|
|
$lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
|
|
// Load a single node.
|
|
|
|
$node = $node_storage->load($result);
|
|
|
|
// check if has translation
|
|
|
|
|
|
|
|
if ($node->hasTranslation($lang)) {
|
|
|
|
// $uuids[] = $result->getField('uuid')->getValues()[0];
|
|
|
|
$nids[] = $result;
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:30:14 +01:00
|
|
|
}
|
|
|
|
// $resp['uuids'] = $uuids;
|
|
|
|
$resp['nids'] = $nids;
|
|
|
|
$resp['count'] = $this->count;
|
|
|
|
$resp['infos'] = t('Please use the search form to search from our @count materials.', array(
|
|
|
|
"@count" => $resp['count']
|
|
|
|
));
|
2019-06-01 16:15:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JsonResponse($resp);
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:38:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for regular page search.
|
|
|
|
*/
|
|
|
|
function pageHandler(Request $request){
|
|
|
|
// \Drupal::logger('materio_sapi')->notice(print_r($request, true));
|
|
|
|
|
|
|
|
$this->parseRequest($request);
|
|
|
|
|
|
|
|
$resp = [
|
|
|
|
"#title" => 'Base'
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->keys) {
|
|
|
|
$resp['#title'] = $this->keys;
|
|
|
|
|
|
|
|
$this->sapiQuery();
|
|
|
|
|
|
|
|
$node_view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
|
|
|
|
|
|
|
|
$items = $this->results->getResultItems();
|
|
|
|
$this->items = [];
|
|
|
|
foreach ($items as $item) {
|
2020-02-18 15:53:21 +01:00
|
|
|
// \Drupal::logger('materio_sapi')->notice(print_r($item, true));
|
2019-06-04 22:38:44 +02:00
|
|
|
try {
|
|
|
|
/** @var \Drupal\Core\Entity\EntityInterface $entity */
|
|
|
|
$entity = $item->getOriginalObject()->getValue();
|
|
|
|
}
|
|
|
|
catch (SearchApiException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$entity) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: define dynamicly viewmode
|
|
|
|
$this->items[] = $node_view_builder->view($entity, 'teaser');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$resp['items'] = $this->items;
|
|
|
|
}else{
|
2021-01-19 16:30:14 +01:00
|
|
|
$resp['#markup'] = t("no keys to search for");
|
2019-06-04 22:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $resp;
|
|
|
|
}
|
2019-06-01 16:15:08 +02:00
|
|
|
}
|