class Search

Same name in this branch
  1. 9 core/modules/search/src/Plugin/views/filter/Search.php \Drupal\search\Plugin\views\filter\Search
  2. 9 core/lib/Drupal/Core/Render/Element/Search.php \Drupal\Core\Render\Element\Search
Same name and namespace in other branches
  1. 11.x core/modules/search/src/Attribute/Search.php \Drupal\search\Attribute\Search
  2. 11.x core/modules/search/src/Plugin/views/filter/Search.php \Drupal\search\Plugin\views\filter\Search
  3. 11.x core/modules/search/src/Plugin/views/argument/Search.php \Drupal\search\Plugin\views\argument\Search
  4. 11.x core/lib/Drupal/Core/Render/Element/Search.php \Drupal\Core\Render\Element\Search

Argument handler for search keywords.

Plugin annotation

@ViewsArgument("search");

Hierarchy

Expanded class hierarchy of Search

Related topics

103 string references to 'Search'
AddHandler::buildForm in core/modules/views_ui/src/Form/Ajax/AddHandler.php
AjaxFormCacheTest::testBlockForms in core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php
Tests AJAX forms in blocks.
block.block.bartik_search.yml in core/profiles/standard/config/optional/block.block.bartik_search.yml
core/profiles/standard/config/optional/block.block.bartik_search.yml
block.block.bartik_search.yml in core/profiles/standard/config/optional/block.block.bartik_search.yml
core/profiles/standard/config/optional/block.block.bartik_search.yml
block.block.claro_help_search.yml in core/modules/help_topics/config/optional/block.block.claro_help_search.yml
core/modules/help_topics/config/optional/block.block.claro_help_search.yml

... See full list

File

core/modules/search/src/Plugin/views/argument/Search.php, line 18

Namespace

Drupal\search\Plugin\views\argument
View source
class Search extends ArgumentPluginBase {
  
  /**
   * A search query to use for parsing search keywords.
   *
   * @var \Drupal\search\ViewsSearchQuery
   */
  protected $searchQuery = NULL;
  
  /**
   * The search type name (value of {search_index}.type in the database).
   *
   * @var string
   */
  protected $searchType;
  
  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->searchType = $this->definition['search_type'];
  }
  
  /**
   * Sets up and parses the search query.
   *
   * @param string $input
   *   The search keywords entered by the user.
   */
  protected function queryParseSearchExpression($input) {
    if (!isset($this->searchQuery)) {
      $this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')
        ->extend(ViewsSearchQuery::class);
      $this->searchQuery
        ->searchExpression($input, $this->searchType);
      $this->searchQuery
        ->publicParseSearchExpression();
    }
  }
  
  /**
   * {@inheritdoc}
   */
  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 = $this->view->query
        ->getConnection()
        ->condition('AND');
      // Create a new join to relate the 'search_total' table to our current 'search_index' table.
      $definition = [
        '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);
      // Add the search score field to the query.
      $this->search_score = $this->query
        ->addField('', "{$search_index}.score * {$search_total}.count", 'score', [
        'function' => 'sum',
      ]);
      // Add the conditions set up by the search query to the views query.
      $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) {
        // Make sure we just look at real conditions.
        if (is_numeric($key)) {
          // Replace the conditions with the table alias of views.
          $this->searchQuery
            ->conditionReplaceString('d.', "{$search_dataset}.", $condition);
        }
      }
      $search_conditions =& $search_condition->conditions();
      $search_conditions = array_merge($search_conditions, $condition_conditions);
      // Add the keyword conditions, as is done in
      // SearchQuery::prepareAndNormalize(), but simplified because we are
      // only concerned with relevance ranking so we do not need to normalize.
      $or = $this->view->query
        ->getConnection()
        ->condition('OR');
      foreach ($words as $word) {
        $or->condition("{$search_index}.word", $word);
      }
      $search_condition->condition($or);
      // Add the GROUP BY and HAVING expressions to the query.
      $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}", [
        $placeholder => $matches,
      ]);
    }
    // Set to NULL to prevent PDO exception when views object is cached
    // and to clear out memory.
    $this->searchQuery = NULL;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.