Skip to content

Commit d569ecd

Browse files
langelandskurfuerst
authored andcommitted
[FEATURE] Suggestion support
adapted from mocdk@c7d340d
1 parent 8c832c4 commit d569ecd

5 files changed

Lines changed: 110 additions & 3 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
namespace Flowpack\SearchPlugin\Controller;
3+
4+
/* *
5+
* This script belongs to the TYPO3 Flow package "Flowpack.SearchPlugin". *
6+
* *
7+
* It is free software; you can redistribute it and/or modify it under *
8+
* the terms of the GNU Lesser General Public License, either version 3 *
9+
* of the License, or (at your option) any later version. *
10+
* *
11+
* The TYPO3 project - inspiring people to share! *
12+
* */
13+
14+
use TYPO3\Flow\Annotations as Flow;
15+
use TYPO3\Flow\Mvc\Controller\ActionController;
16+
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
17+
18+
/**
19+
* Class SuggestController
20+
*
21+
* @author Jon Klixbüll Langeland <jon@moc.net>
22+
* @package Flowpack\SearchPlugin\Controller
23+
*/
24+
class SuggestController extends ActionController {
25+
26+
/**
27+
* @Flow\Inject
28+
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient
29+
*/
30+
protected $elasticSearchClient;
31+
32+
/**
33+
* The node inside which searching should happen
34+
*
35+
* @var NodeInterface
36+
*/
37+
protected $contextNode;
38+
39+
/**
40+
* @Flow\Inject
41+
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
42+
*/
43+
protected $logger;
44+
45+
/**
46+
* @var boolean
47+
*/
48+
protected $logThisQuery = FALSE;
49+
50+
/**
51+
* @var string
52+
*/
53+
protected $logMessage;
54+
55+
/**
56+
* @var array
57+
*/
58+
protected $viewFormatToObjectNameMap = array(
59+
'json' => 'TYPO3\Flow\Mvc\View\JsonView'
60+
);
61+
62+
/**
63+
* @param NodeInterface $node
64+
* @param string $term
65+
*/
66+
public function indexAction(NodeInterface $node, $term) {
67+
$request = array(
68+
'suggests' => array(
69+
'text' => $term,
70+
'term' => array(
71+
'field' => '_all'
72+
)
73+
)
74+
);
75+
76+
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_suggest', array(), json_encode($request))->getTreatedContent();
77+
$suggestions = array_map(function($option) {
78+
return $option['text'];
79+
}, $response['suggests'][0]['options']);
80+
81+
$this->view->assign('value', $suggestions);
82+
}
83+
84+
}

Configuration/Policy.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
privilegeTargets:
2+
TYPO3\Flow\Security\Authorization\Privilege\Method\MethodPrivilege:
3+
Flowpack_SearchPlugin_Controller_SuggestController:
4+
matcher: method(Flowpack\SearchPlugin\Controller\SuggestController->indexAction())
5+
6+
roles:
7+
'TYPO3.Flow:Everybody':
8+
privileges:
9+
-
10+
privilegeTarget: Flowpack_SearchPlugin_Controller_SuggestController
11+
permission: GRANT

Configuration/Routes.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DataSourceController routes
2+
-
3+
name: 'flowpack/searchplugin - SuggestController->index'
4+
uriPattern: 'flowpack/searchplugin'
5+
defaults:
6+
'@package': 'Flowpack.SearchPlugin'
7+
'@controller': 'Suggest'
8+
'@action': 'index'
9+
'@format': 'json'
10+
appendExceedingArguments: TRUE
11+
httpMethods: ['GET']
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form action="" method="GET">
2-
<input type="text" name="search" value="{searchWord}" />
3-
<button type="submit">Send</button>
1+
<form method="GET">
2+
<input type="search" name="search" value="{searchWord}" placeholder="Search" data-autocomplete-source="{f:uri.action(action: 'index', controller: 'Suggest', package: 'Flowpack.SearchPlugin', format: 'json' absolute: 1, arguments: {node: node})}" />
3+
<button type="submit">Search</button>
44
</form>

Resources/Private/TypoScript/SearchPlugin.ts2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ prototype(Flowpack.SearchPlugin:Search) {
3030

3131

3232
prototype(Flowpack.SearchPlugin:Search.Form) < prototype(TYPO3.TypoScript:Template) {
33+
node = ${site}
3334
templatePath = 'resource://Flowpack.SearchPlugin/Private/Templates/NodeTypes/Search.Form.html'
3435
searchWord = ${request.arguments.search}
3536
}

0 commit comments

Comments
 (0)