You are here

protected function HelpSearch::findResults in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::findResults()

Finds the search results.

Return value

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

1 call to HelpSearch::findResults()
HelpSearch::execute in core/modules/help_topics/src/Plugin/Search/HelpSearch.php
Executes the search.

File

core/modules/help_topics/src/Plugin/Search/HelpSearch.php, line 196

Class

HelpSearch
Handles searching for help using the Search module index.

Namespace

Drupal\help_topics\Plugin\Search

Code

protected function findResults() {

  // We need to check access for the current user to see the topics that
  // could be returned by search. Each entry in the help_search_items
  // database has an optional permission that comes from the HelpSection
  // plugin, in addition to the generic 'access administration pages'
  // permission. In order to enforce these permissions so only topics that
  // the current user has permission to view are selected by the query, make
  // a list of the permission strings and pre-check those permissions.
  $this
    ->addCacheContexts([
    'user.permissions',
  ]);
  if (!$this->account
    ->hasPermission('access administration pages')) {
    return NULL;
  }
  $permissions = $this->database
    ->select('help_search_items', 'hsi')
    ->distinct()
    ->fields('hsi', [
    'permission',
  ])
    ->condition('permission', '', '<>')
    ->execute()
    ->fetchCol();
  $denied_permissions = array_filter($permissions, function ($permission) {
    return !$this->account
      ->hasPermission($permission);
  });
  $query = $this->database
    ->select('search_index', 'i')
    ->condition('i.langcode', $this->languageManager
    ->getCurrentLanguage()
    ->getId())
    ->extend(SearchQuery::class)
    ->extend(PagerSelectExtender::class);
  $query
    ->innerJoin('help_search_items', 'hsi', '[i].[sid] = [hsi].[sid] AND [i].[type] = :type', [
    ':type' => $this
      ->getType(),
  ]);
  if ($denied_permissions) {
    $query
      ->condition('hsi.permission', $denied_permissions, 'NOT IN');
  }
  $query
    ->searchExpression($this
    ->getKeywords(), $this
    ->getType());
  $find = $query
    ->fields('i', [
    'langcode',
  ])
    ->fields('hsi', [
    'section_plugin_id',
    'topic_id',
  ])
    ->groupBy('i.langcode')
    ->groupBy('hsi.section_plugin_id')
    ->groupBy('hsi.topic_id')
    ->limit(10)
    ->execute();

  // Check query status and set messages if needed.
  $status = $query
    ->getStatus();
  if ($status & SearchQuery::EXPRESSIONS_IGNORED) {
    $this->messenger
      ->addWarning($this
      ->t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', [
      '@count' => $this->searchSettings
        ->get('and_or_limit'),
    ]));
  }
  if ($status & SearchQuery::LOWER_CASE_OR) {
    $this->messenger
      ->addWarning($this
      ->t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
  }
  if ($status & SearchQuery::NO_POSITIVE_KEYWORDS) {
    $this->messenger
      ->addWarning($this
      ->formatPlural($this->searchSettings
      ->get('index.minimum_word_size'), 'You must include at least one keyword to match in the content, and punctuation is ignored.', 'You must include at least one keyword to match in the content. Keywords must be at least @count characters, and punctuation is ignored.'));
  }
  $unindexed = $this->state
    ->get('help_search_unindexed_count', 1);
  if ($unindexed) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Help search is not fully indexed. Some results may be missing or incorrect.'));
  }
  return $find;
}