View source
<?php
namespace Drupal\search\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class Search extends ArgumentPluginBase {
protected $searchQuery = NULL;
protected $searchType;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->searchType = $this->definition['search_type'];
}
protected function queryParseSearchExpression($input) {
if (!isset($this->searchQuery)) {
$this->searchQuery = db_select('search_index', 'i', array(
'target' => 'replica',
))
->extend('Drupal\\search\\ViewsSearchQuery');
$this->searchQuery
->searchExpression($input, $this->searchType);
$this->searchQuery
->publicParseSearchExpression();
}
}
public function query($group_by = FALSE) {
$required = FALSE;
$this
->queryParseSearchExpression($this->argument);
if (!isset($this->searchQuery)) {
$required = TRUE;
}
else {
$words = $this->searchQuery
->words();
if (empty($words)) {
$required = TRUE;
}
}
if ($required) {
if ($this->operator == 'required') {
$this->query
->addWhere(0, 'FALSE');
}
}
else {
$search_index = $this
->ensureMyTable();
$search_condition = db_and();
$definition = array(
'table' => 'search_total',
'field' => 'word',
'left_table' => $search_index,
'left_field' => 'word',
);
$join = Views::pluginManager('join')
->createInstance('standard', $definition);
$search_total = $this->query
->addRelationship('search_total', $join, $search_index);
$this->search_score = $this->query
->addField('', "{$search_index}.score * {$search_total}.count", 'score', array(
'function' => 'sum',
));
$search_condition
->condition("{$search_index}.type", $this->searchType);
$search_dataset = $this->query
->addTable('node_search_dataset');
$conditions = $this->searchQuery
->conditions();
$condition_conditions =& $conditions
->conditions();
foreach ($condition_conditions as $key => &$condition) {
if (is_numeric($key)) {
$this->searchQuery
->conditionReplaceString('d.', "{$search_dataset}.", $condition);
}
}
$search_conditions =& $search_condition
->conditions();
$search_conditions = array_merge($search_conditions, $condition_conditions);
$or = db_or();
foreach ($words as $word) {
$or
->condition("{$search_index}.word", $word);
}
$search_condition
->condition($or);
$this->query
->addWhere(0, $search_condition);
$this->query
->addGroupBy("{$search_index}.sid");
$matches = $this->searchQuery
->matches();
$placeholder = $this
->placeholder();
$this->query
->addHavingExpression(0, "COUNT(*) >= {$placeholder}", array(
$placeholder => $matches,
));
}
$this->searchQuery = NULL;
}
}