You are here

public function SearchApiPageController::page in Search API Pages 8

Page callback.

Parameters

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

string $search_api_page_name: The search api page name.

string $keys: The search word.

Return value

array The page render array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\search_api\SearchApiException

File

src/Controller/SearchApiPageController.php, line 78

Class

SearchApiPageController
Defines a controller to serve search pages.

Namespace

Drupal\search_api_page\Controller

Code

public function page(Request $request, $search_api_page_name, $keys = '') {

  /* @var $search_api_page \Drupal\search_api_page\SearchApiPageInterface */
  $search_api_page = $this
    ->entityTypeManager()
    ->getStorage('search_api_page')
    ->load($search_api_page_name);

  // Keys can be in the query.
  if (!$search_api_page
    ->getCleanUrl()) {
    $keys = $request
      ->get('keys');
  }
  $build['#theme'] = 'search_api_page';
  if ($search_api_page
    ->showSearchForm()) {
    $build = $this
      ->addSearchForm($build, $search_api_page, $keys);
  }
  if (empty($keys) && !$search_api_page
    ->showAllResultsWhenNoSearchIsPerformed()) {
    return $this
      ->finishBuild($build, $search_api_page);
  }
  $query = $this
    ->prepareQuery($request, $search_api_page);
  if (!empty($keys)) {
    $query
      ->keys($keys);
  }
  $result = $query
    ->execute();

  /* @var $items \Drupal\search_api\Item\ItemInterface[] */
  $items = $result
    ->getResultItems();
  $results = [];
  foreach ($items as $item) {
    $rendered = $this
      ->createItemRenderArray($item, $search_api_page);
    if ($rendered === []) {
      continue;
    }
    $results[] = $rendered;
  }
  if (empty($results)) {
    return $this
      ->finishBuildWithoutResults($build, $search_api_page);
  }
  return $this
    ->finishBuildWithResults($build, $result, $results, $search_api_page);
}