Skip to content

Commit

Permalink
Merge pull request #57 from CuBoulder/issue/56
Browse files Browse the repository at this point in the history
Fixes bug with site frontpage setting (v2.8.4)
  • Loading branch information
jcsparks authored Jun 27, 2024
2 parents 89f9ced + 1f4f9a3 commit 4def4d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
47 changes: 34 additions & 13 deletions src/Form/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$configuration = $this->service->getConfiguration();
$settings = $this->service->getSettings();
$systemSiteSettings = $this->config('system.site');
$siteFrontpage = $systemSiteSettings->get('page.front');
$siteTypeOptions = $configuration->get('site_type_options');
$siteAffiliationOptions = array_filter($configuration->get('site_affiliation_options'), function ($value) {
return !$value['type_restricted'];
Expand All @@ -130,7 +131,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['site_frontpage'] = [
'#type' => 'textfield',
'#title' => $this->t('Home page'),
'#default_value' => $this->aliasManager->getAliasByPath($systemSiteSettings->get('page.front')),
'#default_value' => $siteFrontpage && $siteFrontpage[0] === '/' ? $this->aliasManager->getAliasByPath($siteFrontpage) : $siteFrontpage,
'#required' => TRUE,
'#size' => 40,
'#description' => $this->t('Specify a relative URL to display as the site home page.'),
Expand Down Expand Up @@ -196,6 +197,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];
$siteSearchOptions = $configuration->get('site_search_options');
$siteSearchEnabled = $settings->get('site_search_enabled');
$siteSearchUrl = $settings->get('site_search_url');
$advanced['site_search_enabled'] = [
'#type' => 'fieldset',
'#title' => $this->t('Enable searching'),
Expand Down Expand Up @@ -235,7 +237,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$advanced['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')) : '',
'#default_value' => $siteSearchUrl && $siteSearchUrl[0] === '/' ? $this->aliasManager->getAliasByPath($siteSearchUrl) : $siteSearchUrl,
'#states' => [
'required' => [[':input[name="site_search_enabled_custom"]' => ['checked' => TRUE]]],
],
Expand All @@ -254,27 +256,46 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return parent::buildForm($form, $form_state);
}

/**
* Validates a path to a page on the site.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $fieldName
* The name of the form field containing the path to validate.
*
* @return bool
* Whether the form field contains a valid path.
*/
protected function validatePath(FormStateInterface $form_state, $fieldName) {
$value = $form_state->getValue($fieldName);
if ($value) {
if ($value[0] !== '/') {
$form_state->setErrorByName($fieldName, $this->t("The path '%path' has to start with a slash.", ['%path' => $value]));
return FALSE;
}
if ($this->pathValidator->isValid($value)) {
return TRUE;
}
$form_state->setErrorByName($fieldName, $this->t("The path '%path' is invalid.", ['%path' => $value]));
}
return FALSE;
}

/**
* {@inheritdoc}
*
* @see \Drupal\system\Form\SiteInformationForm::validateForm
* Contains the validation of a home page path.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
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 ($this->validatePath($form_state, 'site_frontpage')) {
$form_state->setValue('site_frontpage', $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
}
if ($this->user->hasPermission('edit ucb site advanced') && $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')]));
}
$this->validatePath($form_state, 'site_search_url');
}
parent::validateForm($form, $form_state);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ucb_site_configuration.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CU Boulder Site Configuration
description: 'Allows CU Boulder site administrators to configure site-specific settings.'
core_version_requirement: ^9 || ^10
type: module
version: '2.8.3'
version: '2.8.4'
package: CU Boulder
dependencies:
- block
Expand Down

0 comments on commit 4def4d5

Please sign in to comment.