Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds search frontend and settings #29

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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