From 032fa00d25165827f7741cf67e8ae250a45a02c2 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Wed, 26 Jun 2024 09:44:02 -0600 Subject: [PATCH 1/2] CuBoulder/ucb_site_configuration#56 Fixes bug with site frontpage setting --- src/Form/GeneralForm.php | 50 ++++++++++++++++++++++++--------- ucb_site_configuration.info.yml | 2 +- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index c27789c..2e1bc18 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -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']; @@ -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.'), @@ -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'), @@ -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]]], ], @@ -254,6 +256,33 @@ 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)) { + $form_state->setErrorByName($fieldName, $this->t("The path '%path' is invalid.", ['%path' => $value])); + return FALSE; + } + return TRUE; + } + return FALSE; + } + /** * {@inheritdoc} * @@ -261,20 +290,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { * 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->validatePath($form_state, 'site_frontpage')) { + $form_state->setValue('site_frontpage', $this->aliasManager->getPathByAlias($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->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')])); - } + if ($this->user->hasPermission('edit ucb site advanced') && $form_state->getValues('site_search_enabled')['site_search_enabled_custom'] && $this->validatePath($form_state, 'site_search_url')) { + $form_state->setValue('site_search_url', $this->aliasManager->getPathByAlias($form_state->getValue('site_search_url'))); } + parent::validateForm($form, $form_state); } /** diff --git a/ucb_site_configuration.info.yml b/ucb_site_configuration.info.yml index 29346c6..71baa46 100644 --- a/ucb_site_configuration.info.yml +++ b/ucb_site_configuration.info.yml @@ -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 From 1f4f9a3c1d39be03a513f1fa61ba5f5afd33c8be Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Wed, 26 Jun 2024 09:51:08 -0600 Subject: [PATCH 2/2] CuBoulder/ucb_site_configuration#56 Cleans up logic --- src/Form/GeneralForm.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index 2e1bc18..125730c 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -274,11 +274,10 @@ protected function validatePath(FormStateInterface $form_state, $fieldName) { $form_state->setErrorByName($fieldName, $this->t("The path '%path' has to start with a slash.", ['%path' => $value])); return FALSE; } - if (!$this->pathValidator->isValid($value)) { - $form_state->setErrorByName($fieldName, $this->t("The path '%path' is invalid.", ['%path' => $value])); - return FALSE; + if ($this->pathValidator->isValid($value)) { + return TRUE; } - return TRUE; + $form_state->setErrorByName($fieldName, $this->t("The path '%path' is invalid.", ['%path' => $value])); } return FALSE; } @@ -293,8 +292,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) { 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'] && $this->validatePath($form_state, 'site_search_url')) { - $form_state->setValue('site_search_url', $this->aliasManager->getPathByAlias($form_state->getValue('site_search_url'))); + if ($this->user->hasPermission('edit ucb site advanced') && $form_state->getValues('site_search_enabled')['site_search_enabled_custom']) { + $this->validatePath($form_state, 'site_search_url'); } parent::validateForm($form, $form_state); }