diff --git a/src/Plugin/views/filter/SearchApiFilterNumeric.php b/src/Plugin/views/filter/SearchApiFilterNumeric.php index 9f67b30..74cec48 100644 --- a/src/Plugin/views/filter/SearchApiFilterNumeric.php +++ b/src/Plugin/views/filter/SearchApiFilterNumeric.php @@ -34,7 +34,7 @@ class SearchApiFilterNumeric extends NumericFilter { /** * {@inheritdoc} */ - protected function opEmpty() { + protected function opEmpty($field) { $this->query->condition($this->realField, NULL, $this->operator == 'empty' ? '=' : '<>', $this->options['group']); } diff --git a/src/Plugin/views/filter/SearchApiFilterTrait.php b/src/Plugin/views/filter/SearchApiFilterTrait.php index 6cc782d..cb62d5a 100644 --- a/src/Plugin/views/filter/SearchApiFilterTrait.php +++ b/src/Plugin/views/filter/SearchApiFilterTrait.php @@ -15,13 +15,6 @@ use Drupal\search_api\Plugin\views\query\SearchApiQuery; trait SearchApiFilterTrait { /** - * The Views query the filter will work with. - * - * @var \Drupal\search_api\Plugin\views\query\SearchApiQuery - */ - public $query; - - /** * Constructs a filter object. */ public function __construct() { @@ -67,8 +60,8 @@ trait SearchApiFilterTrait { * loaded. */ protected function getIndex() { - if ($this->query) { - return $this->query->getIndex(); + if ($this->getQuery()) { + return $this->getQuery()->getIndex(); } $base_table = $this->view->storage->get('base_table'); return SearchApiQuery::getIndexFromTable($base_table); @@ -89,11 +82,21 @@ trait SearchApiFilterTrait { // @todo Use "IN"/"NOT IN" instead, once available. $conjunction = $this->operator == 'or' ? 'OR' : 'AND'; $operator = $this->operator == 'not' ? '<>' : '='; - $filter = $this->query->createFilter($conjunction); + $filter = $this->getQuery()->createFilter($conjunction); foreach ($this->value as $value) { $filter->condition($this->realField, $value, $operator); } - $this->query->filter($filter, $this->options['group']); + $this->getQuery()->filter($filter, $this->options['group']); + } + + /** + * Retrieves the query plugin. + * + * @return \Drupal\search_api\Plugin\views\query\SearchApiQuery + * The query plugin. + */ + public function getQuery() { + return $this->query; } } diff --git a/src/Plugin/views/filter/SearchApiFulltext.php b/src/Plugin/views/filter/SearchApiFulltext.php index 8747a6b..e5b2a6a 100644 --- a/src/Plugin/views/filter/SearchApiFulltext.php +++ b/src/Plugin/views/filter/SearchApiFulltext.php @@ -11,6 +11,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\search_api\Entity\Index; +use Drupal\views\Plugin\views\filter\FilterPluginBase; /** * Defines a filter for adding a fulltext search to the view. @@ -19,39 +20,26 @@ use Drupal\search_api\Entity\Index; * * @ViewsFilter("search_api_fulltext") */ -class SearchApiFulltext extends SearchApiFilterText { +class SearchApiFulltext extends FilterPluginBase { + + use SearchApiFilterTrait; /** * {@inheritdoc} */ public function showOperatorForm(&$form, FormStateInterface $form_state) { - $this->operatorForm($form, $form_state); + parent::showOperatorForm($form, $form_state); $form['operator']['#description'] = $this->t('This operator only applies when using "Search keys" as the "Use as" setting.'); } /** * {@inheritdoc} */ - public function operators() { + public function operatorOptions($which = 'title') { return array( - 'and' => array( - 'title' => $this->t('Contains all of these words'), - 'method' => 'opFulltextSearch', - 'short' => $this->t('and'), - 'values' => 1, - ), - 'or' => array( - 'title' => $this->t('Contains any of these words'), - 'method' => 'opFulltextSearch', - 'short' => $this->t('or'), - 'values' => 1, - ), - 'not' => array( - 'title' => $this->t('Contains none of these words'), - 'method' => 'opFulltextSearch', - 'short' => $this->t('not'), - 'values' => 1, - ), + 'and' => $this->t('Contains all of these words'), + 'or' => $this->t('Contains any of these words'), + 'not' => $this->t('Contains none of these words'), ); } @@ -120,6 +108,20 @@ class SearchApiFulltext extends SearchApiFilterText { /** * {@inheritdoc} */ + protected function valueForm(&$form, FormStateInterface $form_state) { + parent::valueForm($form, $form_state); + + $form['value'] = array( + '#type' => 'textfield', + '#title' => !$form_state->get('exposed') ? $this->t('Value') : '', + '#size' => 30, + '#default_value' => $this->value, + ); + } + + /** + * {@inheritdoc} + */ public function validateExposed(&$form, FormStateInterface $form_state) { // Only validate exposed input. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) { @@ -132,7 +134,7 @@ class SearchApiFulltext extends SearchApiFilterText { } $identifier = $this->options['expose']['identifier']; - $input = &$form_state->getValues()[$identifier]; + $input = &$form_state->getValue($identifier, ''); if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) { $this->operator = $this->options['group_info']['group_items'][$input]['operator']; @@ -161,7 +163,7 @@ class SearchApiFulltext extends SearchApiFilterText { /** * {@inheritdoc} */ - public function opFulltextSearch() { + public function query() { while (is_array($this->value)) { $this->value = $this->value ? reset($this->value) : ''; }