You are here

protected function SearchApiPageController::prepareQuery in Search API Pages 8

Prepares the search query.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

\Drupal\search_api_page\SearchApiPageInterface $search_api_page: The search api page.

Return value

\Drupal\search_api\Query\QueryInterface The prepared query.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\search_api\SearchApiException

1 call to SearchApiPageController::prepareQuery()
SearchApiPageController::page in src/Controller/SearchApiPageController.php
Page callback.

File

src/Controller/SearchApiPageController.php, line 238

Class

SearchApiPageController
Defines a controller to serve search pages.

Namespace

Drupal\search_api_page\Controller

Code

protected function prepareQuery(Request $request, SearchApiPageInterface $search_api_page) {

  /* @var $search_api_index \Drupal\search_api\IndexInterface */
  $search_api_index = $this
    ->entityTypeManager()
    ->getStorage('search_api_index')
    ->load($search_api_page
    ->getIndex());
  $query = $search_api_index
    ->query([
    'limit' => $search_api_page
      ->getLimit(),
    'offset' => $request
      ->get('page') !== NULL ? $request
      ->get('page') * $search_api_page
      ->getLimit() : 0,
  ]);
  $query
    ->setSearchID('search_api_page:' . $search_api_page
    ->id());

  /** @var \Drupal\search_api\ParseMode\ParseModeInterface $parse_mode */
  $parse_mode = $this->parseModePluginManager
    ->createInstance($search_api_page
    ->getParseMode());
  $query
    ->setParseMode($parse_mode);

  // Add filter for current language.
  $langcode = $this
    ->languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $query
    ->setLanguages([
    $langcode,
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $query
    ->setFulltextFields($search_api_page
    ->getSearchedFields());
  return $query;
}