From 04933ca897410f533da85b6fa8f8d22f4119e821 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Wed, 25 Oct 2023 12:19:10 -0600 Subject: [PATCH 1/4] CuBoulder/tiamat-theme#266 adds search settings --- .../ucb_site_configuration.configuration.yml | 8 ++ .../ucb_site_configuration.settings.yml | 4 + src/Form/GeneralForm.php | 61 ++++++++++++- ucb_site_configuration.info.yml | 2 +- ucb_site_configuration.install | 85 ++++++++++++++++--- 5 files changed, 146 insertions(+), 14 deletions(-) diff --git a/config/install/ucb_site_configuration.configuration.yml b/config/install/ucb_site_configuration.configuration.yml index 69701fc..e69cbe9 100644 --- a/config/install/ucb_site_configuration.configuration.yml +++ b/config/install/ucb_site_configuration.configuration.yml @@ -162,6 +162,14 @@ site_affiliation_options: label: 'A CU Boulder Sport Club' type_restricted: true +site_search_options: + custom: + url: '' + label: 'This site' + default: + url: 'https://www.colorado.edu/search' + label: 'All of Colorado.edu' + external_services: mainstay: label: Mainstay diff --git a/config/install/ucb_site_configuration.settings.yml b/config/install/ucb_site_configuration.settings.yml index 7391f01..ce626af 100644 --- a/config/install/ucb_site_configuration.settings.yml +++ b/config/install/ucb_site_configuration.settings.yml @@ -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: [] diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index 90cb1c4..258b192 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -160,7 +160,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('Affiliation'), '#default_value' => $settings->get('site_affiliation'), '#options' => array_merge(['' => $this->t('- None -')], array_map(function ($value) { - return $value['label']; + return $value['label']; }, $siteAffiliationOptions), ['custom' => $this->t('Custom')]), '#required' => FALSE, ], @@ -188,6 +188,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; } 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'), @@ -197,6 +199,44 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#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]]], + ], + '#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); } @@ -215,6 +255,14 @@ public function validateForm(array &$form, FormStateInterface $form_state) { 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')])); + } + } } } @@ -231,11 +279,22 @@ public function submitForm(array &$form, FormStateInterface $form_state) { 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')) { diff --git a/ucb_site_configuration.info.yml b/ucb_site_configuration.info.yml index 47965bd..c6f657f 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.4' +version: '2.5' package: CU Boulder dependencies: - block diff --git a/ucb_site_configuration.install b/ucb_site_configuration.install index 1c98d70..404b7eb 100644 --- a/ucb_site_configuration.install +++ b/ucb_site_configuration.install @@ -8,21 +8,62 @@ use Symfony\Component\Yaml\Yaml; /** - * V2.1 – Adds configuration and settings for the site type and affiliation (CuBoulder/tiamat-theme#210, CuBoulder/tiamat-theme#211). + * Updates the configuration. + * + * @param string[] $configKeys + * The configuration to update. */ -function ucb_site_configuration_update_9502() { - $config = \Drupal::configFactory()->getEditable('ucb_site_configuration.configuration'); - $settings = \Drupal::configFactory()->getEditable('ucb_site_configuration.settings'); - $yaml = Yaml::parse(file_get_contents(drupal_get_path('module', 'ucb_site_configuration') . '/config/install/ucb_site_configuration.configuration.yml')); - if (!is_array($yaml)) { - throw new \RuntimeException('Oh no! Failed to get or parse site type and affiliation configuration YAML.'); +function _ucb_site_configuration_update_config(array $configKeys) { + $modulePath = Drupal::getContainer()->get('extension.path.resolver')->getPath('module', 'ucb_site_configuration'); + $configYaml = Yaml::parse(file_get_contents($modulePath . '/config/install/ucb_site_configuration.configuration.yml')); + $configEditable = \Drupal::configFactory()->getEditable('ucb_site_configuration.configuration'); + foreach ($configKeys as $configKey) { + $configEditable->set($configKey, $configYaml[$configKey]); + } + $configEditable->save(); +} + +/** + * Updates the settings. + * + * Important: This will override any user-defined settings. Recommended only + * for adding previously non-existent settings. + * + * @param string[] $settingKeys + * The settings to update. + */ +function _ucb_site_configuration_update_settings(array $settingKeys) { + $modulePath = Drupal::getContainer()->get('extension.path.resolver')->getPath('module', 'ucb_site_configuration'); + $settingsYaml = Yaml::parse(file_get_contents($modulePath . '/config/install/ucb_site_configuration.settings.yml')); + $settingsEditable = \Drupal::configFactory()->getEditable('ucb_site_configuration.settings'); + foreach ($settingKeys as $settingKey) { + $settingsEditable->set($settingKey, $settingsYaml[$settingKey]); } - $config->set('site_type_options', $yaml['site_type_options'])->set('site_affiliation_options', $yaml['site_affiliation_options'])->save(); - $settings->set('site_type', '')->set('site_affiliation', '')->set('site_affiliation_label', '')->set('site_affiliation_url', '')->save(); + $settingsEditable->save(); +} + +/** + * Adds configuration and settings for the site type and affiliation. + * + * Introduced in version 2.1 to address tiamat-theme#210, tiamat-theme#211. + */ +function ucb_site_configuration_update_9502() { + _ucb_site_configuration_update_config([ + 'site_type_options', + 'site_affiliation_options', + ]); + _ucb_site_configuration_update_settings([ + 'site_type', + 'site_affiliation', + 'site_affiliation_label', + 'site_affiliation_url', + ]); } /** - * V2.1.1 – Removes "Be Boulder slogan" from Appearance settings (CuBoulder/tiamat-theme#230). + * Removes "Be Boulder slogan" from Appearance settings. + * + * Introduced in version 2.1.1 to address tiamat-theme#230. */ function ucb_site_configuration_update_9503() { $config = \Drupal::configFactory()->getEditable('ucb_site_configuration.configuration'); @@ -35,14 +76,18 @@ function ucb_site_configuration_update_9503() { } /** - * V2.2 – Admin Helpscout Beacon moved to ucb_admin_menus (CuBoulder/ucb_admin_menus#2). + * Admin Helpscout Beacon moved to ucb_admin_menus. + * + * Introduced in version 2.2 to address ucb_admin_menus#2. */ function ucb_site_configuration_update_9504() { \Drupal::configFactory()->getEditable('ucb_site_configuration.configuration')->clear('admin_helpscout_beacon_id')->save(); } /** - * V2.3 – Modififies contact info settings (CuBoulder/tiamat-theme#269). + * Modifies contact info settings. + * + * Introduced in version 2.3 to address tiamat-theme#269. */ function ucb_site_configuration_update_9505() { \Drupal::configFactory()->getEditable('ucb_site_configuration.contact_info') @@ -58,3 +103,19 @@ function ucb_site_configuration_update_9505() { ]) ->save(); } + +/** + * Adds configuration and settings for the site search. + * + * Introduced in version 2.5 to address tiamat-theme#266. + */ +function ucb_site_configuration_update_9506() { + _ucb_site_configuration_update_config([ + 'site_search_options', + ]); + _ucb_site_configuration_update_settings([ + 'site_search_enabled', + 'site_search_label', + 'site_search_url', + ]); +} From 890f36b5eb686871cc35904233937c77fc17bc87 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Wed, 25 Oct 2023 15:09:01 -0600 Subject: [PATCH 2/4] CuBoulder/tiamat-theme#266 adds search placeholder setting; splits off "pages & search" into a separate tab --- .../ucb_site_configuration.configuration.yml | 2 + .../SiteSettingsAccessController.php | 29 -- src/Form/GeneralForm.php | 262 +++++------------- src/Form/PagesForm.php | 230 +++++++++++++++ ucb_site_configuration.install | 1 + ucb_site_configuration.links.menu.yml | 14 +- ucb_site_configuration.links.task.yml | 11 +- ucb_site_configuration.routing.yml | 12 +- 8 files changed, 328 insertions(+), 233 deletions(-) delete mode 100644 src/Controller/SiteSettingsAccessController.php create mode 100644 src/Form/PagesForm.php diff --git a/config/install/ucb_site_configuration.configuration.yml b/config/install/ucb_site_configuration.configuration.yml index e69cbe9..e313407 100644 --- a/config/install/ucb_site_configuration.configuration.yml +++ b/config/install/ucb_site_configuration.configuration.yml @@ -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: diff --git a/src/Controller/SiteSettingsAccessController.php b/src/Controller/SiteSettingsAccessController.php deleted file mode 100644 index 4aa5270..0000000 --- a/src/Controller/SiteSettingsAccessController.php +++ /dev/null @@ -1,29 +0,0 @@ -hasPermission('edit ucb site general') || $account->hasPermission('edit ucb site pages')); - } - -} diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index 258b192..f1512b1 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -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 { @@ -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. * @@ -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; } @@ -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') ); } @@ -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); } diff --git a/src/Form/PagesForm.php b/src/Form/PagesForm.php new file mode 100644 index 0000000..29192ad --- /dev/null +++ b/src/Form/PagesForm.php @@ -0,0 +1,230 @@ +user = $user; + $this->aliasManager = $alias_manager; + $this->pathValidator = $path_validator; + $this->requestContext = $request_context; + $this->service = $service; + } + + /** + * {@inheritdoc} + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The container that allows getting any needed services. + * + * @link https://www.drupal.org/node/2133171 For more on dependency injection + */ + 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') + ); + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return ['system.site', 'ucb_site_configuration.settings']; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'ucb_site_configuration_pages_form'; + } + + /** + * {@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'); + $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 site 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_placeholder'] = [ + '#type' => 'textfield', + '#title' => $this->t('Placeholder'), + '#default_value' => $settings->get('site_search_placeholder'), + '#placeholder' => $siteSearchOptions['custom']['placeholder'], + '#required' => FALSE, + '#size' => 32, + '#description' => $this->t('Leave blank to use the default placeholder.'), + ]; + $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]]], + ], + '#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 (($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(); + $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_search_enabled', $siteSearchEnabled) + ->set('site_search_label', $siteSearchFormValues['site_search_label']) + ->set('site_search_placeholder', $siteSearchFormValues['site_search_placeholder']) + ->set('site_search_url', $siteSearchFormValues['site_search_url']) + ->save(); + $this->config('system.site')->set('page.front', $form_state->getValue('site_frontpage'))->save(); + parent::submitForm($form, $form_state); + } + +} diff --git a/ucb_site_configuration.install b/ucb_site_configuration.install index 404b7eb..7c6fa1c 100644 --- a/ucb_site_configuration.install +++ b/ucb_site_configuration.install @@ -116,6 +116,7 @@ function ucb_site_configuration_update_9506() { _ucb_site_configuration_update_settings([ 'site_search_enabled', 'site_search_label', + 'site_search_placeholder', 'site_search_url', ]); } diff --git a/ucb_site_configuration.links.menu.yml b/ucb_site_configuration.links.menu.yml index f0c38f9..a7e8454 100644 --- a/ucb_site_configuration.links.menu.yml +++ b/ucb_site_configuration.links.menu.yml @@ -7,7 +7,7 @@ ucb_site_configuration: ucb_site_configuration.general_form: title: General - description: 'Modify general settings and information such as the site name, type, and affiliation.' + description: 'Modify general settings or information about the site.' route_name: ucb_site_configuration.general_form parent: ucb_site_configuration weight: 0 @@ -17,21 +17,27 @@ ucb_site_configuration.appearance_form: route_name: ucb_site_configuration.appearance_form parent: ucb_site_configuration weight: 1 +ucb_site_configuration.pages_form: + title: Pages & search + description: 'Change the site home page or search options.' + route_name: ucb_site_configuration.pages_form + parent: ucb_site_configuration + weight: 2 ucb_site_configuration.contact_info_form: title: Contact info description: 'Modify the contact info for the site as it appears in the site footer.' route_name: ucb_site_configuration.contact_info_form parent: ucb_site_configuration - weight: 2 + weight: 3 ucb_site_configuration.related_articles_form: title: Related articles description: 'Exclude categories or tags from related articles.' route_name: ucb_site_configuration.related_articles_form parent: ucb_site_configuration - weight: 3 + weight: 4 ucb_site_configuration.external_services: title: Third-party services description: 'Configure third-party services to be used on the site.' route_name: entity.ucb_external_service_include.collection parent: ucb_site_configuration - weight: 4 + weight: 5 diff --git a/ucb_site_configuration.links.task.yml b/ucb_site_configuration.links.task.yml index 2e6fe57..b3a6ecd 100644 --- a/ucb_site_configuration.links.task.yml +++ b/ucb_site_configuration.links.task.yml @@ -8,18 +8,23 @@ ucb_site_configuration.appearance_form: title: 'Appearance' base_route: ucb_site_configuration weight: 1 +ucb_site_configuration.pages_form: + route_name: ucb_site_configuration.pages_form + title: 'Pages & search' + base_route: ucb_site_configuration + weight: 2 ucb_site_configuration.contact_info_form: route_name: ucb_site_configuration.contact_info_form title: 'Contact info' base_route: ucb_site_configuration - weight: 2 + weight: 3 ucb_site_configuration.related_articles_form: route_name: ucb_site_configuration.related_articles_form title: 'Related articles' base_route: ucb_site_configuration - weight: 3 + weight: 4 ucb_site_configuration.external_services: route_name: entity.ucb_external_service_include.collection title: 'Third-party services' base_route: ucb_site_configuration - weight: 4 + weight: 5 diff --git a/ucb_site_configuration.routing.yml b/ucb_site_configuration.routing.yml index 0b142b5..fb9a54e 100644 --- a/ucb_site_configuration.routing.yml +++ b/ucb_site_configuration.routing.yml @@ -16,7 +16,7 @@ ucb_site_configuration.general_form: _form: '\Drupal\ucb_site_configuration\Form\GeneralForm' _title: General requirements: - _custom_access: '\Drupal\ucb_site_configuration\Controller\SiteSettingsAccessController::accessGeneral' + _permission: edit ucb site general options: _admin_route: true @@ -30,6 +30,16 @@ ucb_site_configuration.appearance_form: options: _admin_route: true +ucb_site_configuration.pages_form: + path: '/admin/config/cu-boulder/pages' + defaults: + _form: '\Drupal\ucb_site_configuration\Form\PagesForm' + _title: Pages & search + requirements: + _permission: edit ucb site pages + options: + _admin_route: true + ucb_site_configuration.contact_info_form: path: '/admin/config/cu-boulder/contact-info' defaults: From 7155de5eb36112bea747f1f81085ba3d9957d3c8 Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Thu, 26 Oct 2023 15:21:27 -0600 Subject: [PATCH 3/4] CuBoulder/tiamat-theme#266 exposes `site_search` to theme; adds minor cleanup --- src/Form/GeneralForm.php | 14 +------------- src/Form/PagesForm.php | 14 +------------- src/SiteConfiguration.php | 22 ++++++++++++++++++++++ ucb_site_configuration.links.menu.yml | 8 ++++---- ucb_site_configuration.links.task.yml | 4 ++-- ucb_site_configuration.routing.yml | 4 ++-- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index f1512b1..1e1e3b0 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -5,7 +5,6 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Session\AccountInterface; use Drupal\ucb_site_configuration\SiteConfiguration; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -14,13 +13,6 @@ */ class GeneralForm extends ConfigFormBase { - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $user; - /** * The request context. * @@ -40,14 +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\ucb_site_configuration\SiteConfiguration $service * The site configuration service defined in this module. */ - public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $user, SiteConfiguration $service) { + public function __construct(ConfigFactoryInterface $config_factory, SiteConfiguration $service) { parent::__construct($config_factory); - $this->user = $user; $this->service = $service; } @@ -62,7 +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('ucb_site_configuration') ); } diff --git a/src/Form/PagesForm.php b/src/Form/PagesForm.php index 29192ad..ae1ca34 100644 --- a/src/Form/PagesForm.php +++ b/src/Form/PagesForm.php @@ -7,7 +7,6 @@ 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; @@ -17,13 +16,6 @@ */ class PagesForm extends ConfigFormBase { - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $user; - /** * The path alias manager. * @@ -57,8 +49,6 @@ class PagesForm 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 @@ -68,9 +58,8 @@ class PagesForm extends ConfigFormBase { * @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, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context, SiteConfiguration $service) { parent::__construct($config_factory); - $this->user = $user; $this->aliasManager = $alias_manager; $this->pathValidator = $path_validator; $this->requestContext = $request_context; @@ -88,7 +77,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'), diff --git a/src/SiteConfiguration.php b/src/SiteConfiguration.php index 7b9b45b..0fd67a1 100644 --- a/src/SiteConfiguration.php +++ b/src/SiteConfiguration.php @@ -348,6 +348,28 @@ public function attachSiteInformation(array &$variables) { 'url' => '', ]); $variables['site_affiliation'] = array_merge(['id' => $siteAffiliationId], $siteAffiliationAttribs); + $siteSearchOptions = $configuration->get('site_search_options'); + $siteSearchEnabled = $settings->get('site_search_enabled'); + $variables['site_search'] = []; + foreach ($siteSearchEnabled as $name) { + $siteSearchOption = $siteSearchOptions[$name]; + $siteSearchOption['name'] = $name; + if ('custom' == $name) { + $searchLabel = $settings->get('site_search_label'); + if ($searchLabel) { + $siteSearchOption['label'] = $searchLabel; + } + $searchPlaceholder = $settings->get('site_search_placeholder'); + if ($searchPlaceholder) { + $siteSearchOption['placeholder'] = $searchPlaceholder; + } + $searchUrl = $settings->get('site_search_url'); + if ($searchUrl) { + $siteSearchOption['url'] = $searchUrl; + } + } + $variables['site_search'][] = $siteSearchOption; + } } /** diff --git a/ucb_site_configuration.links.menu.yml b/ucb_site_configuration.links.menu.yml index a7e8454..485fdff 100644 --- a/ucb_site_configuration.links.menu.yml +++ b/ucb_site_configuration.links.menu.yml @@ -7,18 +7,18 @@ ucb_site_configuration: ucb_site_configuration.general_form: title: General - description: 'Modify general settings or information about the site.' + description: 'Modify the site name, type, or affiliation.' route_name: ucb_site_configuration.general_form parent: ucb_site_configuration weight: 0 ucb_site_configuration.appearance_form: - title: Appearance - description: 'Change the appearance, layout, and formatting of the site theme.' + title: Appearance and layout + description: 'Change the appearance, layout, or formatting options of the site.' route_name: ucb_site_configuration.appearance_form parent: ucb_site_configuration weight: 1 ucb_site_configuration.pages_form: - title: Pages & search + title: Pages and search description: 'Change the site home page or search options.' route_name: ucb_site_configuration.pages_form parent: ucb_site_configuration diff --git a/ucb_site_configuration.links.task.yml b/ucb_site_configuration.links.task.yml index b3a6ecd..e643296 100644 --- a/ucb_site_configuration.links.task.yml +++ b/ucb_site_configuration.links.task.yml @@ -5,12 +5,12 @@ ucb_site_configuration.general_form: weight: 0 ucb_site_configuration.appearance_form: route_name: ucb_site_configuration.appearance_form - title: 'Appearance' + title: 'Appearance and layout' base_route: ucb_site_configuration weight: 1 ucb_site_configuration.pages_form: route_name: ucb_site_configuration.pages_form - title: 'Pages & search' + title: 'Pages and search' base_route: ucb_site_configuration weight: 2 ucb_site_configuration.contact_info_form: diff --git a/ucb_site_configuration.routing.yml b/ucb_site_configuration.routing.yml index fb9a54e..3b233cc 100644 --- a/ucb_site_configuration.routing.yml +++ b/ucb_site_configuration.routing.yml @@ -24,7 +24,7 @@ ucb_site_configuration.appearance_form: path: '/admin/config/cu-boulder/appearance' defaults: _form: '\Drupal\ucb_site_configuration\Form\AppearanceForm' - _title: Appearance + _title: Appearance and layout requirements: _permission: edit ucb site appearance options: @@ -34,7 +34,7 @@ ucb_site_configuration.pages_form: path: '/admin/config/cu-boulder/pages' defaults: _form: '\Drupal\ucb_site_configuration\Form\PagesForm' - _title: Pages & search + _title: Pages and search requirements: _permission: edit ucb site pages options: From 300b4019bfa6fc232ac843d19306f74957f5fddb Mon Sep 17 00:00:00 2001 From: Timur Tripp Date: Wed, 1 Nov 2023 14:27:44 -0600 Subject: [PATCH 4/4] =?UTF-8?q?CuBoulder/tiamat-theme#266=20corrects=20par?= =?UTF-8?q?ameter=20`cse`=20=E2=86=92=20`keys`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/install/ucb_site_configuration.configuration.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/install/ucb_site_configuration.configuration.yml b/config/install/ucb_site_configuration.configuration.yml index e313407..3dd86dc 100644 --- a/config/install/ucb_site_configuration.configuration.yml +++ b/config/install/ucb_site_configuration.configuration.yml @@ -171,6 +171,7 @@ site_search_options: url: 'https://www.colorado.edu/search' label: 'All of Colorado.edu' placeholder: 'Search Colorado.edu' + parameter: 'cse' external_services: mainstay: