From 6b7e1bc4a0d4a37ed877ab8c14c62b63bda0f630 Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Mon, 27 Oct 2025 13:27:22 -0700 Subject: [PATCH 1/8] Prefer file description field for document link text if set and available --- localgov_base.theme | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index ee8ea2b..4db23af 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -428,11 +428,40 @@ function localgov_base_preprocess_file_link(&$variables): void { if (isset($variables['link']['#title'])) { $document_title = $variables['link']['#title']; + // Check for use_description_as_link_text setting and description field. + $use_description = FALSE; + $description = ''; + if ($media) { - $media_name = $media->get('name')->getString(); - // If the media name is set and different to the - // $variables['link']['#title'], we will use that instead. - $document_title = ($media_name && $media_name !== $document_title) ? $media_name : $document_title; + // Get the current view mode and field display settings. + $view_mode = $variables['element']['#view_mode'] ?? 'default'; + $field_name = $variables['element']['#field_name'] ?? NULL; + + if ($field_name) { + $entity_display = \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplay($media, $view_mode); + $field_display = $entity_display->getComponent($field_name); + + if ($field_display && isset($field_display['settings']['use_description_as_link_text'])) { + $use_description = $field_display['settings']['use_description_as_link_text']; + } + } + + // Get the description from the file field if available. + if ($use_description && $file->_referringItem) { + $description = $file->_referringItem->description ?? ''; + } + + // Use description as link text if setting is enabled and description exists. + if ($use_description && !empty($description)) { + $document_title = $description; + } + else { + // Otherwise, see about using the media name. + $media_name = $media->get('name')->getString(); + // If the media name is set and different to the + // $variables['link']['#title'], we will use that instead. + $document_title = ($media_name && $media_name !== $document_title) ? $media_name : $document_title; + } } $variables['link']['#title'] = [ From ceaa11bffe6083d00c0c99d0ff63d0538f04d7f5 Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Mon, 27 Oct 2025 13:42:42 -0700 Subject: [PATCH 2/8] Update variable initialisation for file link --- localgov_base.theme | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index 4db23af..a5c7cc3 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -428,15 +428,14 @@ function localgov_base_preprocess_file_link(&$variables): void { if (isset($variables['link']['#title'])) { $document_title = $variables['link']['#title']; - // Check for use_description_as_link_text setting and description field. - $use_description = FALSE; - $description = ''; - if ($media) { // Get the current view mode and field display settings. $view_mode = $variables['element']['#view_mode'] ?? 'default'; $field_name = $variables['element']['#field_name'] ?? NULL; + // Initialise this variable as false + $use_description = FALSE; + if ($field_name) { $entity_display = \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplay($media, $view_mode); $field_display = $entity_display->getComponent($field_name); From 2b8bfe64207ffb0f0a5533b1c4596d1df32a7701 Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Tue, 28 Oct 2025 01:17:53 -0700 Subject: [PATCH 3/8] Code improvements --- localgov_base.theme | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index a5c7cc3..b78ebda 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -6,6 +6,7 @@ */ use Drupal\Component\Utility\Crypt; +use Drupal\Core\Entity\EntityDisplayRepository; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\media\MediaInterface; @@ -433,11 +434,12 @@ function localgov_base_preprocess_file_link(&$variables): void { $view_mode = $variables['element']['#view_mode'] ?? 'default'; $field_name = $variables['element']['#field_name'] ?? NULL; - // Initialise this variable as false + // Initialise this variable as false. $use_description = FALSE; if ($field_name) { - $entity_display = \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplay($media, $view_mode); + $entity_display_repository = \Drupal::service('entity_display.repository'); + $entity_display = $entity_display_repository->getViewDisplay($media->getEntityTypeId(), $media->bundle(), $view_mode); $field_display = $entity_display->getComponent($field_name); if ($field_display && isset($field_display['settings']['use_description_as_link_text'])) { From 22510100e62e3f3ce2724b6655a6377d72d9b23e Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Tue, 28 Oct 2025 01:38:57 -0700 Subject: [PATCH 4/8] Fix test violations --- localgov_base.theme | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index b78ebda..dda6779 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -6,7 +6,6 @@ */ use Drupal\Component\Utility\Crypt; -use Drupal\Core\Entity\EntityDisplayRepository; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\media\MediaInterface; @@ -452,7 +451,7 @@ function localgov_base_preprocess_file_link(&$variables): void { $description = $file->_referringItem->description ?? ''; } - // Use description as link text if setting is enabled and description exists. + // Use description as link text if enabled and description exists. if ($use_description && !empty($description)) { $document_title = $description; } From 592e29501562f1703e3481d4e61dc0bea49a4f1e Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Tue, 28 Oct 2025 02:25:08 -0700 Subject: [PATCH 5/8] Try to get the file field --- localgov_base.theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index dda6779..aad7d85 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -429,17 +429,17 @@ function localgov_base_preprocess_file_link(&$variables): void { $document_title = $variables['link']['#title']; if ($media) { - // Get the current view mode and field display settings. - $view_mode = $variables['element']['#view_mode'] ?? 'default'; - $field_name = $variables['element']['#field_name'] ?? NULL; + // Assume some defaults for document media type. + $view_mode = 'default'; + $media_file_field_name = ($media->bundle() === 'document') ? 'field_media_file' : NULL; // Initialise this variable as false. $use_description = FALSE; - if ($field_name) { + if ($media_file_field_name) { $entity_display_repository = \Drupal::service('entity_display.repository'); $entity_display = $entity_display_repository->getViewDisplay($media->getEntityTypeId(), $media->bundle(), $view_mode); - $field_display = $entity_display->getComponent($field_name); + $field_display = $entity_display->getComponent($media_file_field_name); if ($field_display && isset($field_display['settings']['use_description_as_link_text'])) { $use_description = $field_display['settings']['use_description_as_link_text']; From bb19ae790bbb8fa6700c37229542509314b22f1e Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Sat, 1 Nov 2025 03:31:07 -0700 Subject: [PATCH 6/8] Ensure description variable is defined --- localgov_base.theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/localgov_base.theme b/localgov_base.theme index aad7d85..1d08436 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -433,8 +433,9 @@ function localgov_base_preprocess_file_link(&$variables): void { $view_mode = 'default'; $media_file_field_name = ($media->bundle() === 'document') ? 'field_media_file' : NULL; - // Initialise this variable as false. + // Initialise description variables. $use_description = FALSE; + $description = ''; if ($media_file_field_name) { $entity_display_repository = \Drupal::service('entity_display.repository'); @@ -445,7 +446,7 @@ function localgov_base_preprocess_file_link(&$variables): void { $use_description = $field_display['settings']['use_description_as_link_text']; } } - + // Get the description from the file field if available. if ($use_description && $file->_referringItem) { $description = $file->_referringItem->description ?? ''; From 5afa043c03a0370c95a4428d6ae402af5f897e01 Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Sat, 1 Nov 2025 06:59:18 -0700 Subject: [PATCH 7/8] Get the right document field field_media_document --- localgov_base.theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localgov_base.theme b/localgov_base.theme index 1d08436..9b4c159 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -431,7 +431,7 @@ function localgov_base_preprocess_file_link(&$variables): void { if ($media) { // Assume some defaults for document media type. $view_mode = 'default'; - $media_file_field_name = ($media->bundle() === 'document') ? 'field_media_file' : NULL; + $media_file_field_name = ($media->bundle() === 'document') ? 'field_media_document' : NULL; // Initialise description variables. $use_description = FALSE; From 9a7140162437ce65e2cc699590c647b46e70acdc Mon Sep 17 00:00:00 2001 From: Tony Barker <4941711+tonypaulbarker@users.noreply.github.com> Date: Tue, 4 Nov 2025 03:40:34 -0800 Subject: [PATCH 8/8] Remove white space for php 8.4 --- localgov_base.theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localgov_base.theme b/localgov_base.theme index 9b4c159..f6b16f2 100644 --- a/localgov_base.theme +++ b/localgov_base.theme @@ -446,7 +446,7 @@ function localgov_base_preprocess_file_link(&$variables): void { $use_description = $field_display['settings']['use_description_as_link_text']; } } - + // Get the description from the file field if available. if ($use_description && $file->_referringItem) { $description = $file->_referringItem->description ?? '';