You are here

protected function FileSearch::findResults in Search File Attachments 8

Queries to find search results, and sets status messages.

This method can assume that $this->isSearchExecutable() has already been checked and returned TRUE.

Return value

\Drupal\Core\Database\StatementInterface|null Results from search query execute() method, or NULL if the search failed.

1 call to FileSearch::findResults()
FileSearch::execute in src/Plugin/Search/FileSearch.php
Executes the search.

File

src/Plugin/Search/FileSearch.php, line 171

Class

FileSearch
Executes a keyword search for files against {file_managed} database table.

Namespace

Drupal\search_file_attachments\Plugin\Search

Code

protected function findResults() {
  $keys = $this->keywords;
  $query = $this->database
    ->select('search_index', 'i', array(
    'target' => 'replica',
  ))
    ->extend('Drupal\\search\\SearchQuery')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->join('file_managed', 'f', 'f.fid = i.sid');
  $query
    ->join('search_dataset', 'sd', 'sd.sid = i.sid AND sd.type = i.type');
  $query
    ->searchExpression($keys, $this
    ->getPluginId());

  // Run the query.
  $find = $query
    ->fields('i', array(
    'langcode',
  ))
    ->fields('sd', array(
    'data',
  ))
    ->groupBy('i.langcode')
    ->groupBy('sd.data')
    ->limit(10)
    ->execute();

  // Check query status and set messages if needed.
  $status = $query
    ->getStatus();
  if ($status & SearchQuery::EXPRESSIONS_IGNORED) {
    drupal_set_message($this
      ->t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', array(
      '@count' => $this->searchSettings
        ->get('and_or_limit'),
    )), 'warning');
  }
  if ($status & SearchQuery::LOWER_CASE_OR) {
    drupal_set_message($this
      ->t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'), 'warning');
  }
  if ($status & SearchQuery::NO_POSITIVE_KEYWORDS) {
    drupal_set_message($this
      ->formatPlural($this->searchSettings
      ->get('index.minimum_word_size'), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.'), 'warning');
  }
  return $find;
}