Skip to content

Commit

Permalink
feat(IYY-263): Add new controller for dynamic search page
Browse files Browse the repository at this point in the history
  • Loading branch information
codechefmarc committed Nov 19, 2024
1 parent 00f131c commit 1733237
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ focus_header_image: ''
site_name_image: ''
search:
enable_search_form: 1
enable_cas_search: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Drupal\ys_core\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\Markup;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* Controller for the search page.
*/
class SearchController extends ControllerBase {

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a SearchController object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}

/**
* Dynamic search page to show CAS titles or not.
*/
public function searchPage(Request $request) {
// Check your configuration value to decide which view to render.
$config = $this->configFactory->get('ys_core.header_settings');
$view_name = $config->get('search.enable_cas_search') ? 'search_cas' : 'search';

$view = Views::getView($view_name);
if ($view) {
// Set the display (e.g., 'default' display or a custom display).
$view->setDisplay('default');

// Retrieve the 'keywords' parameter from the request, if it exists.
$keywords = $request->query->get('keywords');
if ($keywords) {
// If your view has a contextual filter for keywords, pass it here.
$view->setArguments([$keywords]);
$keywords_markup = Markup::create('<em>' . $this->t('@keywords', ['@keywords' => $keywords]) . '</em>');
$title = $this->t('Search results: @keywords', ['@keywords' => $keywords_markup]);
}
else {
$title = $this->t('Search');
}

// Execute the view and render it.
$view->preExecute();
$view->execute();

return [
'#title' => $title,
'view' => $view->render(),
'#cache' => [
'contexts' => ['url.query_args:keywords'],
],
];
}

return [
'#markup' => $this->t('No view available.'),
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ public function buildForm(array $form, FormStateInterface $form_state) {
],
];

$form['site_search_container']['enable_cas_search'] = [
'#type' => 'checkbox',
'#description' => $this->t('When enabled, anonymous users can see titles only of CAS-only content in search.'),
'#title' => $this->t('Enable CAS search'),
'#default_value' => $headerConfig->get('search.enable_cas_search'),
'#states' => [
'invisible' => [
':input[name="enable_search_form"]' => ['checked' => FALSE],
],
],
];

$form['full_screen_homepage_image_container']['focus_header_image'] = [
'#type' => 'media_library',
'#allowed_bundles' => ['image'],
Expand Down Expand Up @@ -365,6 +377,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$headerConfig->set('cta_content', $form_state->getValue('cta_content'));
$headerConfig->set('cta_url', $form_state->getValue('cta_url'));
$headerConfig->set('search.enable_search_form', $form_state->getValue('enable_search_form'));
$headerConfig->set('search.enable_cas_search', $form_state->getValue('enable_cas_search'));
$headerConfig->set('focus_header_image', $form_state->getValue('focus_header_image'));

$headerConfig->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ ys_core.admin_views_settings:
_title: 'Views Settings'
requirements:
_permission: 'yalesites manage settings'
# Dynamic search page.
ys_core.search_page:
path: '/search'
defaults:
_controller: '\Drupal\ys_core\Controller\SearchController::searchPage'
requirements:
_permission: 'TRUE'

0 comments on commit 1733237

Please sign in to comment.