SearchViewController.php in Google Search Appliance 8
File
src/Controller/SearchViewController.php
View source
<?php
namespace Drupal\google_appliance\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\google_appliance\SearchResults\SearchQuery;
use Drupal\google_appliance\Service\SearchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\google_appliance\Form\SearchForm;
class SearchViewController extends ControllerBase {
protected $search;
public function __construct(SearchInterface $search, FormBuilderInterface $formBuilder) {
$this->formBuilder = $formBuilder;
$this->search = $search;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('google_appliance.search'), $container
->get('form_builder'));
}
public function get(Request $request, $search_query = '', $result_sort = NULL) {
$search_query = urldecode($search_query);
$form = $this->formBuilder
->getForm(SearchForm::class, $search_query);
if ($search_query !== '' && !$request->request
->has('form_id')) {
$response = $this->search
->search(new SearchQuery($search_query, $result_sort === 'date' ? SearchQuery::ORDER_DATE : NULL, $request->query
->get('page')));
return [
'#theme' => 'google_appliance_search_results',
'#results' => $response,
'#form' => $form,
'#cache' => [
'tags' => [
'config:google_appliance.settings',
],
],
];
}
return $form;
}
}