3 namespace Drupal\search\Plugin\views\argument;
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ViewExecutable;
9 use Drupal\views\Views;
12 * Argument handler for search keywords.
14 * @ingroup views_argument_handlers
16 * @ViewsArgument("search")
18 class Search extends ArgumentPluginBase {
21 * A search query to use for parsing search keywords.
23 * @var \Drupal\search\ViewsSearchQuery
25 protected $searchQuery = NULL;
28 * The search type name (value of {search_index}.type in the database).
32 protected $searchType;
37 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
38 parent::init($view, $display, $options);
40 $this->searchType = $this->definition['search_type'];
44 * Sets up and parses the search query.
46 * @param string $input
47 * The search keywords entered by the user.
49 protected function queryParseSearchExpression($input) {
50 if (!isset($this->searchQuery)) {
51 $this->searchQuery = db_select('search_index', 'i', ['target' => 'replica'])->extend('Drupal\search\ViewsSearchQuery');
52 $this->searchQuery->searchExpression($input, $this->searchType);
53 $this->searchQuery->publicParseSearchExpression();
60 public function query($group_by = FALSE) {
62 $this->queryParseSearchExpression($this->argument);
63 if (!isset($this->searchQuery)) {
67 $words = $this->searchQuery->words();
73 if ($this->operator == 'required') {
74 $this->query->addWhere(0, 'FALSE');
78 $search_index = $this->ensureMyTable();
80 $search_condition = new Condition('AND');
82 // Create a new join to relate the 'search_total' table to our current 'search_index' table.
84 'table' => 'search_total',
86 'left_table' => $search_index,
87 'left_field' => 'word',
89 $join = Views::pluginManager('join')->createInstance('standard', $definition);
90 $search_total = $this->query->addRelationship('search_total', $join, $search_index);
92 // Add the search score field to the query.
93 $this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', ['function' => 'sum']);
95 // Add the conditions set up by the search query to the views query.
96 $search_condition->condition("$search_index.type", $this->searchType);
97 $search_dataset = $this->query->addTable('node_search_dataset');
98 $conditions = $this->searchQuery->conditions();
99 $condition_conditions =& $conditions->conditions();
100 foreach ($condition_conditions as $key => &$condition) {
101 // Make sure we just look at real conditions.
102 if (is_numeric($key)) {
103 // Replace the conditions with the table alias of views.
104 $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition);
107 $search_conditions =& $search_condition->conditions();
108 $search_conditions = array_merge($search_conditions, $condition_conditions);
110 // Add the keyword conditions, as is done in
111 // SearchQuery::prepareAndNormalize(), but simplified because we are
112 // only concerned with relevance ranking so we do not need to normalize.
113 $or = new Condition('OR');
114 foreach ($words as $word) {
115 $or->condition("$search_index.word", $word);
117 $search_condition->condition($or);
119 // Add the GROUP BY and HAVING expressions to the query.
120 $this->query->addWhere(0, $search_condition);
121 $this->query->addGroupBy("$search_index.sid");
122 $matches = $this->searchQuery->matches();
123 $placeholder = $this->placeholder();
124 $this->query->addHavingExpression(0, "COUNT(*) >= $placeholder", [$placeholder => $matches]);
127 // Set to NULL to prevent PDO exception when views object is cached
128 // and to clear out memory.
129 $this->searchQuery = NULL;