3 namespace Drupal\search\Plugin\views\argument;
5 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
6 use Drupal\views\Plugin\views\display\DisplayPluginBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Views;
11 * Argument handler for search keywords.
13 * @ingroup views_argument_handlers
15 * @ViewsArgument("search")
17 class Search extends ArgumentPluginBase {
20 * A search query to use for parsing search keywords.
22 * @var \Drupal\search\ViewsSearchQuery
24 protected $searchQuery = NULL;
27 * The search type name (value of {search_index}.type in the database).
31 protected $searchType;
36 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
37 parent::init($view, $display, $options);
39 $this->searchType = $this->definition['search_type'];
43 * Sets up and parses the search query.
45 * @param string $input
46 * The search keywords entered by the user.
48 protected function queryParseSearchExpression($input) {
49 if (!isset($this->searchQuery)) {
50 $this->searchQuery = db_select('search_index', 'i', ['target' => 'replica'])->extend('Drupal\search\ViewsSearchQuery');
51 $this->searchQuery->searchExpression($input, $this->searchType);
52 $this->searchQuery->publicParseSearchExpression();
59 public function query($group_by = FALSE) {
61 $this->queryParseSearchExpression($this->argument);
62 if (!isset($this->searchQuery)) {
66 $words = $this->searchQuery->words();
72 if ($this->operator == 'required') {
73 $this->query->addWhere(0, 'FALSE');
77 $search_index = $this->ensureMyTable();
79 $search_condition = db_and();
81 // Create a new join to relate the 'search_total' table to our current 'search_index' table.
83 'table' => 'search_total',
85 'left_table' => $search_index,
86 'left_field' => 'word',
88 $join = Views::pluginManager('join')->createInstance('standard', $definition);
89 $search_total = $this->query->addRelationship('search_total', $join, $search_index);
91 // Add the search score field to the query.
92 $this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', ['function' => 'sum']);
94 // Add the conditions set up by the search query to the views query.
95 $search_condition->condition("$search_index.type", $this->searchType);
96 $search_dataset = $this->query->addTable('node_search_dataset');
97 $conditions = $this->searchQuery->conditions();
98 $condition_conditions =& $conditions->conditions();
99 foreach ($condition_conditions as $key => &$condition) {
100 // Make sure we just look at real conditions.
101 if (is_numeric($key)) {
102 // Replace the conditions with the table alias of views.
103 $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition);
106 $search_conditions =& $search_condition->conditions();
107 $search_conditions = array_merge($search_conditions, $condition_conditions);
109 // Add the keyword conditions, as is done in
110 // SearchQuery::prepareAndNormalize(), but simplified because we are
111 // only concerned with relevance ranking so we do not need to normalize.
113 foreach ($words as $word) {
114 $or->condition("$search_index.word", $word);
116 $search_condition->condition($or);
118 // Add the GROUP BY and HAVING expressions to the query.
119 $this->query->addWhere(0, $search_condition);
120 $this->query->addGroupBy("$search_index.sid");
121 $matches = $this->searchQuery->matches();
122 $placeholder = $this->placeholder();
123 $this->query->addHavingExpression(0, "COUNT(*) >= $placeholder", [$placeholder => $matches]);
126 // Set to NULL to prevent PDO exception when views object is cached
127 // and to clear out memory.
128 $this->searchQuery = NULL;