Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/install/ucb_site_configuration.settings.yml
Original file line number Diff line number Diff line change
@@ -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: ''
Expand Down
10 changes: 10 additions & 0 deletions src/Form/GeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions ucb_site_configuration.install
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}
40 changes: 40 additions & 0 deletions ucb_site_configuration.module
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down