Skip to content

Commit

Permalink
Merge pull request #29 from CuBoulder/issue/tiamat-theme-266
Browse files Browse the repository at this point in the history
Adds search frontend and settings
  • Loading branch information
jcsparks authored and web-flow committed Nov 2, 2023
2 parents 9853d09 + 300b401 commit 130e4a8
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 203 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- ### Adds search frontend and settings
This update:
- Adds a search modal which appears when clicking on the search icon in the top bar.
- Adds a new "Pages and search" tab to CU Boulder site settings (`/admin/config/cu-boulder/pages`). This tab contains settings accessible to Architect, Developer, and Site Manager:
- The home page setting (moved from "General").
- Options to enable site search, all of Colorado.edu search (default), both, or neither.
- Configuration for the site search label, placeholder, and URL.
- Renames "Appearance" to "Appearance and layout" and alters the descriptions of menu items.
- Adds the [Google Programmable Search Engine](https://www.drupal.org/project/google_cse) module, which allows creating custom search pages to use with site search.

CuBoulder/tiamat-theme#266

Sister PR in: [tiamat-theme](https://github.com/CuBoulder/tiamat-theme/pull/527), [tiamat10-profile](https://github.com/CuBoulder/tiamat10-profile/pull/43), [tiamat10-project-template](https://github.com/CuBoulder/tiamat10-project-template/pull/17)
---

- ### Adds home page setting to CU Boulder site settings
This update to CU Boulder Site Configuration:
- Adds a new home page setting to CU Boulder site settings. Resolves CuBoulder/tiamat-theme#506
Expand Down
11 changes: 11 additions & 0 deletions config/install/ucb_site_configuration.configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ site_affiliation_options:
label: 'A CU Boulder Sport Club'
type_restricted: true

site_search_options:
custom:
url: ''
label: 'This site'
placeholder: 'Search this site'
default:
url: 'https://www.colorado.edu/search'
label: 'All of Colorado.edu'
placeholder: 'Search Colorado.edu'
parameter: 'cse'

external_services:
mainstay:
label: Mainstay
Expand Down
4 changes: 4 additions & 0 deletions config/install/ucb_site_configuration.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ site_type: ''
site_affiliation: ''
site_affiliation_label: ''
site_affiliation_url: ''
site_search_enabled:
- default
site_search_label: ''
site_search_url: ''

related_articles_enabled_by_default: FALSE
related_articles_exclude_categories: []
Expand Down
29 changes: 0 additions & 29 deletions src/Controller/SiteSettingsAccessController.php

This file was deleted.

215 changes: 66 additions & 149 deletions src/Form/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,14 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\ucb_site_configuration\SiteConfiguration;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* The form for the "General" tab in CU Boulder site settings.
*
* While "Pages" could eventually be split off into its own tab, the settings
* are found under "General", at least for now. "General" and "Pages" require
* different permissions to access.
*/
class GeneralForm extends ConfigFormBase {

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;

/**
* The path alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;

/**
* The path validator.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;

/**
* The request context.
*
Expand All @@ -61,23 +32,11 @@ class GeneralForm extends ConfigFormBase {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Session\AccountInterface $user
* The current user.
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* The path alias manager.
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* The path validator.
* @param \Drupal\Core\Routing\RequestContext $request_context
* The request context.
* @param \Drupal\ucb_site_configuration\SiteConfiguration $service
* The site configuration service defined in this module.
*/
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $user, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context, SiteConfiguration $service) {
public function __construct(ConfigFactoryInterface $config_factory, SiteConfiguration $service) {
parent::__construct($config_factory);
$this->user = $user;
$this->aliasManager = $alias_manager;
$this->pathValidator = $path_validator;
$this->requestContext = $request_context;
$this->service = $service;
}

Expand All @@ -92,10 +51,6 @@ public function __construct(ConfigFactoryInterface $config_factory, AccountInter
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('current_user'),
$container->get('path_alias.manager'),
$container->get('path.validator'),
$container->get('router.request_context'),
$container->get('ucb_site_configuration')
);
}
Expand All @@ -116,131 +71,93 @@ public function getFormId() {

/**
* {@inheritdoc}
*
* @see \Drupal\system\Form\SiteInformationForm::buildForm
* Contains the definition of a home page field.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$configuration = $this->service->getConfiguration();
$settings = $this->service->getSettings();
$systemSiteSettings = $this->config('system.site');
if ($this->user->hasPermission('edit ucb site general')) {
$siteTypeOptions = $configuration->get('site_type_options');
$siteAffiliationOptions = array_filter($configuration->get('site_affiliation_options'), function ($value) {
return !$value['type_restricted'];
});
$form['site_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Site name'),
'#default_value' => $systemSiteSettings->get('name'),
'#required' => TRUE,
];
$form['site_type'] = [
$siteTypeOptions = $configuration->get('site_type_options');
$siteAffiliationOptions = array_filter($configuration->get('site_affiliation_options'), function ($value) {
return !$value['type_restricted'];
});
$form['site_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Site name'),
'#default_value' => $systemSiteSettings->get('name'),
'#required' => TRUE,
];
$form['site_type'] = [
'#type' => 'select',
'#title' => $this->t('Type'),
'#default_value' => $settings->get('site_type'),
'#options' => array_merge(['' => $this->t('- None -')], array_map(function ($value) {
return $value['label'];
}, $siteTypeOptions)),
'#required' => FALSE,
];
$affiliationHidesOn = [];
foreach ($siteTypeOptions as $siteTypeId => $siteType) {
if (isset($siteType['affiliation'])) {
array_push($affiliationHidesOn, [':input[name="site_type"]' => ['value' => $siteTypeId]]);
}
}
$form['site_affiliation_container'] = [
'#type' => 'container',
'#states' => [
'invisible' => $affiliationHidesOn,
],
'site_affiliation' => [
'#type' => 'select',
'#title' => $this->t('Type'),
'#default_value' => $settings->get('site_type'),
'#title' => $this->t('Affiliation'),
'#default_value' => $settings->get('site_affiliation'),
'#options' => array_merge(['' => $this->t('- None -')], array_map(function ($value) {
return $value['label'];
}, $siteTypeOptions)),
return $value['label'];
}, $siteAffiliationOptions), ['custom' => $this->t('Custom')]),
'#required' => FALSE,
];
$affiliationHidesOn = [];
foreach ($siteTypeOptions as $siteTypeId => $siteType) {
if (isset($siteType['affiliation'])) {
array_push($affiliationHidesOn, [':input[name="site_type"]' => ['value' => $siteTypeId]]);
}
}
$form['site_affiliation_container'] = [
'#type' => 'container',
],
'site_affiliation_custom' => [
'#type' => 'fieldset',
'#description' => $this->t('Define a title and optional URL for the custom affiliation.'),
'#states' => [
'invisible' => $affiliationHidesOn,
'visible' => [[':input[name="site_affiliation"]' => ['value' => 'custom']]],
],
'site_affiliation' => [
'#type' => 'select',
'#title' => $this->t('Affiliation'),
'#default_value' => $settings->get('site_affiliation'),
'#options' => array_merge(['' => $this->t('- None -')], array_map(function ($value) {
return $value['label'];
}, $siteAffiliationOptions), ['custom' => $this->t('Custom')]),
'site_affiliation_label' => [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#default_value' => $settings->get('site_affiliation_label'),
'#required' => FALSE,
'#maxlength' => 255,
],
'site_affiliation_custom' => [
'#type' => 'fieldset',
'#description' => $this->t('Define a title and optional URL for the custom affiliation.'),
'#states' => [
'visible' => [[':input[name="site_affiliation"]' => ['value' => 'custom']]],
],
'site_affiliation_label' => [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#default_value' => $settings->get('site_affiliation_label'),
'#required' => FALSE,
'#maxlength' => 255,
],
'site_affiliation_url' => [
'#type' => 'textfield',
'#title' => $this->t('URL'),
'#default_value' => $settings->get('site_affiliation_url'),
'#required' => FALSE,
'#maxlength' => 255,
],
'site_affiliation_url' => [
'#type' => 'textfield',
'#title' => $this->t('URL'),
'#default_value' => $settings->get('site_affiliation_url'),
'#required' => FALSE,
'#maxlength' => 255,
],
];
}
if ($this->user->hasPermission('edit ucb site pages')) {
$form['site_frontpage'] = [
'#type' => 'textfield',
'#title' => $this->t('Home page'),
'#default_value' => $this->aliasManager->getAliasByPath($systemSiteSettings->get('page.front')),
'#required' => TRUE,
'#size' => 40,
'#description' => $this->t('Specify a relative URL to display as the home page.'),
'#field_prefix' => $this->requestContext->getCompleteBaseUrl(),
];
}
],
];
return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*
* @see \Drupal\system\Form\SiteInformationForm::validateForm
* Contains the validation of a home page path.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this->user->hasPermission('edit ucb site pages')) {
if (($value = $form_state->getValue('site_frontpage')) && $value[0] !== '/') {
$form_state->setErrorByName('site_frontpage', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_frontpage')]));
}
if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
$form_state->setErrorByName('site_frontpage', $this->t("The path '%path' is invalid.", ['%path' => $form_state->getValue('site_frontpage')]));
}
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$configuration = $this->service->getConfiguration();
if ($this->user->hasPermission('edit ucb site general')) {
$siteTypeOptions = $configuration->get('site_type_options');
$this->config('system.site')->set('name', $form_state->getValue('site_name'))->save();
$siteTypeId = $form_state->getValue('site_type');
$siteAffiliationId = $form_state->getValue('site_affiliation');
if ($siteTypeId && isset($siteTypeOptions[$siteTypeId]) && isset($siteTypeOptions[$siteTypeId]['affiliation'])) {
$siteAffiliationId = $siteTypeOptions[$siteTypeId]['affiliation'];
}
$this->config('ucb_site_configuration.settings')
->set('site_type', $siteTypeId)
->set('site_affiliation', $siteAffiliationId)
->set('site_affiliation_label', $form_state->getValue('site_affiliation_label'))
->set('site_affiliation_url', $form_state->getValue('site_affiliation_url'))
->save();
}
if ($this->user->hasPermission('edit ucb site pages')) {
$this->config('system.site')->set('page.front', $form_state->getValue('site_frontpage'))->save();
$siteTypeOptions = $configuration->get('site_type_options');
$this->config('system.site')->set('name', $form_state->getValue('site_name'))->save();
$siteTypeId = $form_state->getValue('site_type');
$siteAffiliationId = $form_state->getValue('site_affiliation');
if ($siteTypeId && isset($siteTypeOptions[$siteTypeId]) && isset($siteTypeOptions[$siteTypeId]['affiliation'])) {
$siteAffiliationId = $siteTypeOptions[$siteTypeId]['affiliation'];
}
$this->config('ucb_site_configuration.settings')
->set('site_type', $siteTypeId)
->set('site_affiliation', $siteAffiliationId)
->set('site_affiliation_label', $form_state->getValue('site_affiliation_label'))
->set('site_affiliation_url', $form_state->getValue('site_affiliation_url'))
->save();
parent::submitForm($form, $form_state);
}

Expand Down
Loading

0 comments on commit 130e4a8

Please sign in to comment.