From 6c8755a4c79eccc8929b34b92af8a62c2f87c182 Mon Sep 17 00:00:00 2001 From: Joshua Nicholson <94021017+jnicholCU@users.noreply.github.com> Date: Fri, 13 Feb 2026 15:43:42 -0700 Subject: [PATCH] Short site name title tag Add a new `site_name_title_tag` option that allows site managers, devs, architects to change their site's name that shows in the title tag. Preferably a shorter, non-acronym, for SEO purposes. --- .../ucb_site_configuration.settings.yml | 1 + src/Form/GeneralForm.php | 10 +++++ ucb_site_configuration.install | 12 ++++++ ucb_site_configuration.module | 40 +++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/config/install/ucb_site_configuration.settings.yml b/config/install/ucb_site_configuration.settings.yml index 75e2c77..322e65f 100644 --- a/config/install/ucb_site_configuration.settings.yml +++ b/config/install/ucb_site_configuration.settings.yml @@ -1,6 +1,7 @@ # Editable site settings (non-theme) will go here # Settings in "General" +site_name_title_tag: '' site_type: '' site_affiliation: '' site_affiliation_label: '' diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php index d92b11c..6997b2a 100644 --- a/src/Form/GeneralForm.php +++ b/src/Form/GeneralForm.php @@ -129,6 +129,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $systemSiteSettings->get('name'), '#required' => TRUE, ]; + $form['site_name_title_tag'] = [ + '#type' => 'textfield', + '#title' => $this->t('Short name for title tag'), + '#default_value' => $settings->get('site_name_title_tag'), + '#description' => $this->t('An alternate, preferably shorter version of the site name used in HTML title tags. Leave blank to use the full site name above.'), + '#required' => FALSE, + ]; $form['site_frontpage'] = [ '#type' => 'textfield', '#title' => $this->t('Home page'), @@ -340,6 +347,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set('page.front', $form_state->getValue('site_frontpage')) ->set('page.404', $form_state->getValue('site_404')) ->save(); + $this->config('ucb_site_configuration.settings') + ->set('site_name_title_tag', $form_state->getValue('site_name_title_tag')) + ->save(); if ($this->user->hasPermission('edit ucb site advanced')) { $siteTypeId = $form_state->getValue('site_type'); $siteAffiliationId = $form_state->getValue('site_affiliation'); diff --git a/ucb_site_configuration.install b/ucb_site_configuration.install index a3454fd..5c07945 100644 --- a/ucb_site_configuration.install +++ b/ucb_site_configuration.install @@ -250,3 +250,15 @@ function ucb_site_configuration_update_9515() { 'site_affiliation_options', ]); } + +/** + * Adds the site name for title tag setting. + * + * Allows sites to specify a shorter alternate site name for use in HTML + * title tags to prevent excessively long titles. + */ +function ucb_site_configuration_update_9516() { + _ucb_site_configuration_update_settings([ + 'site_name_title_tag', + ]); +} diff --git a/ucb_site_configuration.module b/ucb_site_configuration.module index fda6fa6..31e2af9 100644 --- a/ucb_site_configuration.module +++ b/ucb_site_configuration.module @@ -8,6 +8,46 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeInterface; +/** + * Uses alternate site name when [site:name] token is used and override is set. + * + * Overrides the [site:name] token (used by Metatag and others) with the shorter + * "Site name for title tag" when configured. This keeps title tags from + * becoming excessively long on content pages that use Metatag. + * + * Implements hook_tokens_alter(). + */ +function ucb_site_configuration_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) { + if ($context['type'] !== 'site' || !isset($context['tokens']['name'])) { + return; + } + + $titleTagName = \Drupal::config('ucb_site_configuration.settings')->get('site_name_title_tag'); + if (empty(trim($titleTagName))) { + return; + } + + $replacements[$context['tokens']['name']] = $titleTagName; + $bubbleable_metadata->addCacheTags(['config:ucb_site_configuration.settings']); +} + +/** + * Replaces the site name in head_title for non-Metatag pages (e.g. admin). + * + * On pages where Metatag does not run, Drupal core sets head_title with + * separate 'title' and 'name' keys. This alters the 'name' value when the + * override is configured. + * + * Implements hook_preprocess_HOOK(). + */ +function ucb_site_configuration_preprocess_html(array &$variables) { + $titleTagName = \Drupal::config('ucb_site_configuration.settings')->get('site_name_title_tag'); + if (!empty(trim($titleTagName)) && isset($variables['head_title']['name'])) { + $variables['head_title']['name'] = $titleTagName; + $variables['#cache']['tags'][] = 'config:ucb_site_configuration.settings'; + } +} + /** * Enables site general settings to be referenced from the page template. *