|
@@ -1,5 +1,4 @@
|
|
|
<?php
|
|
|
-// https://www.qed42.com/blog/autocomplete-drupal-8
|
|
|
// https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
|
|
|
|
|
|
namespace Drupal\materio_sapi\Controller;
|
|
@@ -14,73 +13,88 @@ use Drupal\search_api\Entity\Index;
|
|
|
// https://drupal.stackexchange.com/questions/225008/programatically-use-search-api
|
|
|
|
|
|
/**
|
|
|
- * Defines a route controller for entity autocomplete form elements.
|
|
|
+ * Defines a route controller for materio sapi search (regular and ajax).
|
|
|
*/
|
|
|
class Base extends ControllerBase {
|
|
|
|
|
|
- /**
|
|
|
- * Handler for autocomplete request.
|
|
|
- */
|
|
|
- public function getResults(Request $request) {
|
|
|
- $resp = [
|
|
|
- 'tes' => 'ok',
|
|
|
- 'autocomplete' => $request->query->get('autocomplete'),
|
|
|
- 'keys' => $request->query->get('keys'),
|
|
|
- 'range' => array(
|
|
|
- 'offset' => $request->query->get('offset'),
|
|
|
- 'limit' => $request->query->get('limit'),
|
|
|
- ),
|
|
|
- ];
|
|
|
+ private $limit = 15;
|
|
|
+ private $offset = 0;
|
|
|
|
|
|
- // Get the typed string from the URL, if it exists.
|
|
|
- if ($keys = $resp['keys']) {
|
|
|
- $typed_string = Tags::explode($keys);
|
|
|
- $typed_string = Unicode::strtolower($keys);
|
|
|
- \Drupal::logger('materio_sapi')->notice($typed_string);
|
|
|
+ private function sapiQuery(){
|
|
|
+ $this->index = Index::load('database');
|
|
|
+ $this->query = $this->index->query();
|
|
|
|
|
|
- $index = Index::load('database');
|
|
|
- $query = $index->query();
|
|
|
+ // 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);
|
|
|
|
|
|
- // Change the parse mode for the search.
|
|
|
- $parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
|
|
|
- ->createInstance('direct');
|
|
|
- $parse_mode->setConjunction('OR');
|
|
|
- $query->setParseMode($parse_mode);
|
|
|
+ // Set fulltext search keywords and fields.
|
|
|
+ $this->query->keys($this->keys);
|
|
|
+ // $this->query->setFulltextFields(['name']);
|
|
|
|
|
|
- // Set fulltext search keywords and fields.
|
|
|
- $query->keys($typed_string);
|
|
|
- // $query->setFulltextFields(['name']);
|
|
|
+ // Set additional conditions.
|
|
|
+ // $this->query->addCondition('status', 1)
|
|
|
+ // ->addCondition('author', 1, '<>');
|
|
|
|
|
|
- // Set additional conditions.
|
|
|
- // $query->addCondition('status', 1)
|
|
|
- // ->addCondition('author', 1, '<>');
|
|
|
+ // Restrict the search to specific languages.
|
|
|
+ // $this->query->setLanguages(['de', 'it']);
|
|
|
|
|
|
- // Restrict the search to specific languages.
|
|
|
- // $query->setLanguages(['de', 'it']);
|
|
|
+ // Do paging.
|
|
|
+ $this->query->range($this->offset, $this->limit);
|
|
|
|
|
|
- // Do paging.
|
|
|
- $query->range($resp['range']['offset'], $resp['range']['limit']);
|
|
|
+ // Add sorting.
|
|
|
+ $this->query->sort('search_api_relevance', 'DESC');
|
|
|
|
|
|
- // Add sorting.
|
|
|
- $query->sort('search_api_relevance', 'DESC');
|
|
|
+ // 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');
|
|
|
|
|
|
- // Set one or more tags for the query.
|
|
|
- // @see hook_search_api_query_TAG_alter()
|
|
|
- // @see hook_search_api_results_TAG_alter()
|
|
|
- $query->addTag('materio_sapi_search');
|
|
|
+ $this->results = $this->query->execute();
|
|
|
|
|
|
- $results = $query->execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 = [
|
|
|
+ 'keys' => $this->keys,
|
|
|
+ 'term' => $this->term,
|
|
|
+ 'range' => array(
|
|
|
+ 'offset' => $this->offset,
|
|
|
+ 'limit' => $this->limit
|
|
|
+ ),
|
|
|
+ ];
|
|
|
|
|
|
- $resultitems = $results->getResultItems();
|
|
|
- $resp['count'] = $results->getResultCount();
|
|
|
- $resp['resultitems'] = array_keys($resultitems);
|
|
|
- $resp['options'] = $query->getOptions();
|
|
|
+ if ($this->keys) {
|
|
|
+ $this->sapiQuery();
|
|
|
|
|
|
+ $resp['count'] = $this->results->getResultCount();
|
|
|
+ $resp['options'] = $this->query->getOptions();
|
|
|
|
|
|
$items = [];
|
|
|
- foreach ($results as $result) {
|
|
|
- // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('tid')->getValues(),true));
|
|
|
- // \Drupal::logger('materio_sapi')->notice(print_r($result->getField('name')->getValues(),true));
|
|
|
+ foreach ($this->results as $result) {
|
|
|
$nid = $result->getField('nid')->getValues()[0];
|
|
|
$title = $result->getField('title')->getValues()[0]->getText();
|
|
|
$items[] = [
|
|
@@ -94,4 +108,50 @@ class Base extends ControllerBase {
|
|
|
return new JsonResponse($resp);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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) {
|
|
|
+ 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{
|
|
|
+ $resp['#markup'] = "no keys to search for";
|
|
|
+ }
|
|
|
+
|
|
|
+ return $resp;
|
|
|
+ }
|
|
|
}
|