draft of materio_sapi autocomplete

This commit is contained in:
Bachir Soussi Chiadmi 2019-05-29 13:41:34 +02:00
parent ddd80ec288
commit ea2fd59c63
6 changed files with 325 additions and 4 deletions

View File

@ -44,6 +44,8 @@
"drupal/migrate_tools": "4.x-dev", "drupal/migrate_tools": "4.x-dev",
"drupal/mimemail": "1.x-dev", "drupal/mimemail": "1.x-dev",
"drupal/restui": "^1.16", "drupal/restui": "^1.16",
"drupal/search_api_autocomplete": "^1.2",
"drupal/search_api_page": "1.x-dev",
"drupal/search_api_solr": "^3.0", "drupal/search_api_solr": "^3.0",
"drupal/select_translation": "1.x-dev", "drupal/select_translation": "1.x-dev",
"drupal/simplenews": "1.x-dev", "drupal/simplenews": "1.x-dev",

114
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5b5c2c51bef1aa5e0078a95dafa3e5b5", "content-hash": "8a8cacad3f133bac4dc306fda59b40d7",
"packages": [ "packages": [
{ {
"name": "alchemy/zippy", "name": "alchemy/zippy",
@ -8554,6 +8554,63 @@
"irc": "irc://irc.freenode.org/drupal-search-api" "irc": "irc://irc.freenode.org/drupal-search-api"
} }
}, },
{
"name": "drupal/search_api_autocomplete",
"version": "1.2.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/search_api_autocomplete.git",
"reference": "8.x-1.2"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/search_api_autocomplete-8.x-1.2.zip",
"reference": "8.x-1.2",
"shasum": "606908583c1e0aff57a4215355d1d3e07a1d50d2"
},
"require": {
"drupal/core": "~8.0",
"drupal/search_api": "1.x"
},
"require-dev": {
"drupal/search_api_page": "@dev"
},
"type": "drupal-module",
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev"
},
"drupal": {
"version": "8.x-1.2",
"datestamp": "1552333980",
"security-coverage": {
"status": "covered",
"message": "Covered by Drupal's security advisory policy"
}
}
},
"notification-url": "https://packages.drupal.org/8/downloads",
"license": [
"GPL-2.0+"
],
"authors": [
{
"name": "Thomas Seidl",
"homepage": "https://www.drupal.org/u/drunken-monkey"
},
{
"name": "See other contributors",
"homepage": "https://www.drupal.org/node/1142202/committers"
}
],
"description": "Adds autocomplete functionality to searches.",
"homepage": "https://www.drupal.org/project/search_api_autocomplete",
"support": {
"source": "http://git.drupal.org/project/search_api_autocomplete.git",
"issues": "https://www.drupal.org/project/issues/search_api_autocomplete",
"irc": "irc://irc.freenode.org/drupal-search-api"
}
},
{ {
"name": "drupal/search_api_db", "name": "drupal/search_api_db",
"version": "1.13.0", "version": "1.13.0",
@ -8599,6 +8656,60 @@
"source": "https://git.drupalcode.org/project/search_api" "source": "https://git.drupalcode.org/project/search_api"
} }
}, },
{
"name": "drupal/search_api_page",
"version": "dev-1.x",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/search_api_page.git",
"reference": "b0b4e96d85077722aaece1e2e48f1312352a90c4"
},
"require": {
"drupal/core": "~8.0",
"drupal/search_api": "*"
},
"require-dev": {
"drupal/facets": "*"
},
"type": "drupal-module",
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev"
},
"drupal": {
"version": "8.x-1.0-alpha12+4-dev",
"datestamp": "1557232081",
"security-coverage": {
"status": "not-covered",
"message": "Dev releases are not covered by Drupal security advisories."
}
}
},
"notification-url": "https://packages.drupal.org/8/downloads",
"license": [
"GPL-2.0-or-later"
],
"authors": [
{
"name": "borisson_",
"homepage": "https://www.drupal.org/user/2393360"
},
{
"name": "drunken monkey",
"homepage": "https://www.drupal.org/user/205582"
},
{
"name": "swentel",
"homepage": "https://www.drupal.org/user/107403"
}
],
"description": "Create search pages using Search API indexes.",
"homepage": "https://www.drupal.org/project/search_api_page",
"support": {
"source": "https://git.drupalcode.org/project/search_api_page"
},
"time": "2019-05-07T18:19:46+00:00"
},
{ {
"name": "drupal/search_api_solr", "name": "drupal/search_api_solr",
"version": "3.0.0-beta4", "version": "3.0.0-beta4",
@ -17074,6 +17185,7 @@
"drupal/migrate_plus": 20, "drupal/migrate_plus": 20,
"drupal/migrate_tools": 20, "drupal/migrate_tools": 20,
"drupal/mimemail": 20, "drupal/mimemail": 20,
"drupal/search_api_page": 20,
"drupal/select_translation": 20, "drupal/select_translation": 20,
"drupal/simplenews": 20, "drupal/simplenews": 20,
"drupal/telephone_formatter": 10, "drupal/telephone_formatter": 10,

View File

@ -0,0 +1,108 @@
uuid: 89507e1b-4a2c-4e27-9e90-e3278f805e3b
langcode: en
status: true
dependencies:
module:
- search_api_solr
- taxonomy
- search_api
config:
- field.storage.taxonomy_term.field_synonyms
- search_api.server.materio
third_party_settings:
search_api_solr:
finalize: false
commit_before_finalize: false
commit_after_finalize: false
multilingual:
limit_to_content_language: true
include_language_independent: true
highlighter:
maxAnalyzedChars: 51200
fragmenter: gap
regex:
slop: 0.5
pattern: blank
maxAnalyzedChars: 10000
usePhraseHighlighter: true
highlightMultiTerm: true
preserveMulti: false
highlight:
mergeContiguous: false
requireFieldMatch: false
snippets: 3
fragsize: 0
advanced:
index_prefix: ''
id: autocomplete
name: autocomplete
description: ''
read_only: false
field_settings:
field_synonyms:
label: Synonyms
datasource_id: 'entity:taxonomy_term'
property_path: field_synonyms
type: 'solr_text_custom:ngram'
dependencies:
config:
- field.storage.taxonomy_term.field_synonyms
langcode:
label: Language
datasource_id: 'entity:taxonomy_term'
property_path: langcode
type: string
dependencies:
module:
- taxonomy
name:
label: Name
datasource_id: 'entity:taxonomy_term'
property_path: name
type: 'solr_text_custom:ngram'
dependencies:
module:
- taxonomy
status:
label: Published
datasource_id: 'entity:taxonomy_term'
property_path: status
type: boolean
dependencies:
module:
- taxonomy
tid:
label: 'Term ID'
datasource_id: 'entity:taxonomy_term'
property_path: tid
type: integer
dependencies:
module:
- taxonomy
datasource_settings:
'entity:taxonomy_term':
bundles:
default: false
selected:
- tags
- thesaurus
languages:
default: false
selected:
- en
- fr
processor_settings:
add_url: { }
aggregated_field: { }
entity_status: { }
rendered_item: { }
solr_date_range:
weights:
preprocess_index: 0
tracker_settings:
default:
indexing_order: fifo
options:
index_directly: true
cron_limit: 50
server: materio

View File

@ -1,3 +1,10 @@
materio_sapi.search_autocomplete:
path: '/materio_sapi/form/autocomplete'
defaults:
_controller: '\Drupal\materio_sapi\Controller\FormAutocomplete::Autocomplete'
_format: json
requirements:
_permission: 'access materio search'
# #
# materio_sapi.materio_sapi_search_form: # materio_sapi.materio_sapi_search_form:
# path: '/materio_sapi/form/materio_sapi_search' # path: '/materio_sapi/form/materio_sapi_search'

View File

@ -0,0 +1,88 @@
<?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;
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;
/**
* Defines a route controller for entity autocomplete form elements.
*/
class FormAutocomplete extends ControllerBase {
/**
* Handler for autocomplete request.
*/
public function autocomplete(Request $request) {
// Get the typed string from the URL, if it exists.
if ($input = $request->query->get('q')) {
$typed_string = Tags::explode($input);
$typed_string = Unicode::strtolower(array_pop($typed_string));
// \Drupal::logger('materio_sapi')->notice($typed_string);
$index = Index::load('autocomplete');
$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');
$query->setParseMode($parse_mode);
// Set fulltext search keywords and fields.
$query->keys($typed_string);
$query->setFulltextFields(['name']);
// Set additional conditions.
// $query->addCondition('status', 1)
// ->addCondition('author', 1, '<>');
// Restrict the search to specific languages.
// $query->setLanguages(['de', 'it']);
// Do paging.
$query->range(0, 10);
// 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()
$query->addTag('materio_sapi_autocomplete');
// Execute the search.
$results = $query->execute();
// echo "Result count: {$results->getResultCount()}\n";
// $ids = implode(', ', array_keys($results->getResultItems()));
// echo "Returned IDs: $ids.\n";
// $items = $results->getResultItems();
// \Drupal::logger('materio_sapi')->notice($results->getResultCount());
// \Drupal::logger('materio_sapi')->notice(implode(', ', array_keys($items)));
$response = [];
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));
$tid = $result->getField('tid')->getValues()[0];
$term_name = $result->getField('name')->getValues()[0]->getText();
$response[] = [
'value' => $tid,
'label' => $term_name,
];
}
}
return new JsonResponse($response);
}
}

View File

@ -24,14 +24,18 @@ class MaterioSapiSearchForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form['search'] = [ $form['search'] = [
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('Search'), // '#title' => $this->t('Search'),
'#maxlength' => 64, '#maxlength' => 64,
'#size' => 64, '#size' => 25,
'#weight' => '0', '#weight' => '0',
'#attributes' => [
"placeholder" => $this->t('Search'),
],
'#autocomplete_route_name' => 'materio_sapi.search_autocomplete',
]; ];
$form['submit'] = [ $form['submit'] = [
'#type' => 'submit', '#type' => 'submit',
'#value' => $this->t('Submit'), '#value' => $this->t('Search'),
]; ];
return $form; return $form;