Skip to content

Commit

Permalink
CuBoulder/tiamat-theme#266 adds search placeholder setting; splits of…
Browse files Browse the repository at this point in the history
…f "pages & search" into a separate tab
  • Loading branch information
timurtripp committed Oct 25, 2023
1 parent 04933ca commit 890f36b
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 233 deletions.
2 changes: 2 additions & 0 deletions config/install/ucb_site_configuration.configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ 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'

external_services:
mainstay:
Expand Down
29 changes: 0 additions & 29 deletions src/Controller/SiteSettingsAccessController.php

This file was deleted.

262 changes: 66 additions & 196 deletions src/Form/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
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 {

Expand All @@ -28,20 +21,6 @@ class GeneralForm extends ConfigFormBase {
*/
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 @@ -63,21 +42,12 @@ class GeneralForm extends ConfigFormBase {
* 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, AccountInterface $user, 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 @@ -93,9 +63,6 @@ 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,190 +83,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,
],
],
];
}
if ($this->user->hasPermission('edit ucb site pages')) {
$siteSearchOptions = $configuration->get('site_search_options');
$siteSearchEnabled = $settings->get('site_search_enabled');
$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(),
];
$form['site_search_enabled'] = [
'#type' => 'fieldset',
'#title' => $this->t('Enable searching'),
];
foreach ($siteSearchOptions as $key => $value) {
$form['site_search_enabled']['site_search_enabled_' . $key] = [
'#type' => 'checkbox',
'#title' => $value['label'],
'#default_value' => in_array($key, $siteSearchEnabled),
];
}
$form['site_search'] = [
'#type' => 'fieldset',
'#title' => $this->t('Site search'),
'#states' => [
'visible' => [[':input[name="site_search_enabled_custom"]' => ['checked' => TRUE]]],
],
];
$form['site_search']['site_search_label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#default_value' => $settings->get('site_search_label'),
'#placeholder' => $siteSearchOptions['custom']['label'],
'#required' => FALSE,
'#size' => 32,
'#description' => $this->t('Leave blank to use the default label.'),
];
$form['site_search']['site_search_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Search page'),
'#default_value' => $settings->get('site_search_url') ? $this->aliasManager->getAliasByPath($settings->get('site_search_url')) : '',
'#states' => [
'required' => [[':input[name="site_search_enabled_custom"]' => ['checked' => TRUE]]],
'site_affiliation_url' => [
'#type' => 'textfield',
'#title' => $this->t('URL'),
'#default_value' => $settings->get('site_affiliation_url'),
'#required' => FALSE,
'#maxlength' => 255,
],
'#size' => 40,
'#description' => $this->t('Specify a relative URL to use as the site search 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')]));
}
if ($form_state->getValues('site_search_enabled')['site_search_enabled_custom']) {
if (($value = $form_state->getValue('site_search_url')) && $value[0] !== '/') {
$form_state->setErrorByName('site_search_url', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_search_url')]));
}
if (!$this->pathValidator->isValid($form_state->getValue('site_search_url'))) {
$form_state->setErrorByName('site_search_url', $this->t("The path '%path' is invalid.", ['%path' => $form_state->getValue('site_search_url')]));
}
}
}
}

/**
* {@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'];
}
$siteSearchEnabled = [];
$siteSearchEnabledFormValues = $form_state->getValues('site_search_enabled');
$siteSearchFormValues = $form_state->getValues('site_search');
foreach ($configuration->get('site_search_options') as $key => $value) {
if ($siteSearchEnabledFormValues['site_search_enabled_' . $key]) {
$siteSearchEnabled[] = $key;
}
}
$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'))
->set('site_search_enabled', $siteSearchEnabled)
->set('site_search_label', $siteSearchFormValues['site_search_label'])
->set('site_search_url', $siteSearchFormValues['site_search_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 890f36b

Please sign in to comment.