From d0ce6fc694f10eb607a41a34b86bd33ca13c444b Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Thu, 19 Dec 2024 15:38:16 +0200 Subject: [PATCH 1/3] UHF-9507: Refactor the unit contact card logic on hdbt, replace the title display logic with the heading radiobutton selection --- hdbt.theme | 138 +++++++----------- .../paragraph--unit-contact-card.html.twig | 6 +- 2 files changed, 58 insertions(+), 86 deletions(-) diff --git a/hdbt.theme b/hdbt.theme index 84472d8f6..f937ab3d5 100644 --- a/hdbt.theme +++ b/hdbt.theme @@ -938,103 +938,75 @@ function hdbt_preprocess_paragraph__unit_accessibility_information(&$variables) /** * Implements hook_preprocess_paragraph__type(). */ -function hdbt_preprocess_paragraph__unit_contact_card(&$variables) { +function hdbt_preprocess_paragraph__unit_contact_card(array &$variables) : void { $paragraph = $variables['paragraph']; - $langcode = \Drupal::languageManager() - ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); + $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); + // Get the unit entity and ensure it exists and has the correct translation $unit = $paragraph->get('field_unit_contact_unit') ?->first() ?->get('entity') ?->getTarget() ?->getEntity(); - if ($unit instanceof Unit && $unit->hasTranslation($langcode)) { - $unit_translation = $unit->getTranslation($langcode); - $variables['unit_contact_card'] = []; - - // Get title from paragraph settings. - if (!empty($paragraph_title = $paragraph->get('field_unit_contact_title') - ?->view('unit_contact_card'))) { - $variables['unit_contact_card']['title'] = $paragraph_title; - } - else { - $variables['unit_contact_card']['title'] = $unit_translation?->get('name') - ?->view('unit_contact_card'); - } - - // Show address if chosen from paragraph settings. - if ($paragraph->get('field_unit_contact_use_address')->value === '1') { - $variables['unit_contact_card']['address'] = $unit_translation?->get('address') - ?->view('unit_contact_card'); - } - - // Show postal address if chosen from paragraph settings. - if ($paragraph->get('field_unit_contact_use_postal')->value === '1') { - $variables['unit_contact_card']['address_postal'] = $unit_translation?->get('address_postal') - ?->view('unit_contact_card'); - } - - // Show phone number if chosen from paragraph settings. - if ($paragraph->get('field_unit_contact_use_phone')->value === '1') { - $variables['unit_contact_card']['phone'] = $unit_translation?->get('phone') - ?->view('unit_contact_card'); - } - - // Show opening hours if chosen from paragraph settings. - if ($paragraph->get('field_unit_contact_use_opening')->value === '1') { - $variables['unit_contact_card']['opening_hours'] = $unit_translation?->get('opening_hours') - ?->view('unit_contact_card'); - } - - // Show picture if chosen from paragraph settings. - if ($paragraph->get('field_unit_contact_use_picture')->value === '1') { - $variables['unit_contact_card']['picture_url'] = $unit_translation?->get('picture_url') - ?->view('unit_contact_card'); - if ($paragraph->get('field_unit_contact_use_override')->value === '1') { - $variables['unit_contact_card']['picture_url_override'] = $unit_translation?->get('picture_url_override') - ?->view('unit_contact_card'); - } - } + if (!($unit instanceof Unit && $unit->hasTranslation($langcode))) { + return; + } - // Show highlight connections as additional information if chosen from - // paragraph settings. - if ( - $paragraph->get('field_unit_contact_use_details')->value === '1' && - $unit_translation->hasField('highlights') - ) { - $variables['unit_contact_card']['details'] = $unit_translation->get('highlights') - ?->view('unit_contact_card'); - } + $unit_translation = $unit->getTranslation($langcode); + $variables['unit_contact_card'] = []; + + // Save field values to variables for reuse to avoid repeated lookups. + $heading_value = $paragraph->get('field_unit_contact_heading')->value ?? ''; + $use_fields = [ + 'address' => $paragraph->get('field_unit_contact_use_address')->value, + 'address_postal' => $paragraph->get('field_unit_contact_use_postal')->value, + 'phone' => $paragraph->get('field_unit_contact_use_phone')->value, + 'opening_hours' => $paragraph->get('field_unit_contact_use_opening')->value, + 'picture' => $paragraph->get('field_unit_contact_use_picture')->value, + 'highlights' => $paragraph->get('field_unit_contact_use_details')->value, + 'link' => $paragraph->get('field_unit_contact_use_link')->value, + 'picture_override' => $paragraph->get('field_unit_contact_use_override')->value, + ]; - // Show link to unit page if chosen from paragraph settings and unit is - // published. - if ( - $paragraph->get('field_unit_contact_use_link')->value === '1' && - $unit_translation->isPublished() - ) { - $variables['unit_contact_card']['unit_url'] = !$unit_translation->isNew() ? $unit_translation->toUrl('canonical') : NULL; + // Heading is determined with a radiobutton selection. + // If nothing is selected (value is compulsory but can happen) use default + // value Contact information. If the unit name is used check if the + // unit name has been overridden. + if (empty($heading_value) || $heading_value === 'default') { + $variables['unit_contact_card']['heading'] = t('Contact information'); + } elseif ($heading_value === 'unit_name') { + $variables['unit_contact_card']['heading'] = !empty($unit_translation?->get('name_override')->value) + ? $unit_translation?->get('name_override')->view('unit_contact_card') + : $unit_translation?->get('name')->view('unit_contact_card'); + } + + // Fields that have a simple boolean selector to display or hide the fields + // value on the unit contact card. + $simple_fields = ['address', 'address_postal', 'phone', 'opening_hours', 'highlights']; + foreach ($simple_fields as $field) { + if ($use_fields[$field]) { + $variables['unit_contact_card'][$field] = $unit_translation?->get($field)?->view('unit_contact_card'); } + } - // Check if the highlight content is printed on left or right column of - // unit content card. - if ( - ($paragraph->get('field_unit_contact_use_picture')->value === '1') && - ($paragraph->get('field_unit_contact_use_address')->value === '1') && - (empty($variables['unit_contact_card']['address_postal']) && empty($variables['unit_contact_card']['phone']) && empty($variables['unit_contact_card']['opening_hours'])) - ) { - $variables['unit_contact_card']['details_on_left'] = TRUE; + // Picture is also displayed using simple boolean selector, but + // the default picture of the unit can be overridden using the + // picture_url_override field so we need to check for that. + if ($use_fields['picture']) { + $variables['unit_contact_card']['picture_url'] = $unit_translation?->get('picture_url')?->view('unit_contact_card'); + if ($use_fields['picture_override']) { + $variables['unit_contact_card']['picture_url_override'] = $unit_translation?->get('picture_url_override')?->view('unit_contact_card'); } + } - if ( - empty($variables['unit_contact_card']['address']) && - empty($variables['unit_contact_card']['address_postal']) && - empty($variables['unit_contact_card']['phone']) && - empty($variables['unit_contact_card']['opening_hours']) && - empty($variables['unit_contact_card']['unit_url']) - ) { - $variables['unit_contact_card']['left_column_empty'] = TRUE; - } + // Build the unit url to be used in the unit contact card link. + // If the unit is not published the unit_url should be NULL because + // it's not possible for anonymous users to access the unit page. + if ($use_fields['link'] && $unit_translation->isPublished()) { + $variables['unit_contact_card']['unit_url'] = !$unit_translation->isNew() + ? $unit_translation->toUrl('canonical') + : NULL; } } diff --git a/templates/paragraphs/paragraph--unit-contact-card.html.twig b/templates/paragraphs/paragraph--unit-contact-card.html.twig index bf6450a31..6fcc9b0d5 100644 --- a/templates/paragraphs/paragraph--unit-contact-card.html.twig +++ b/templates/paragraphs/paragraph--unit-contact-card.html.twig @@ -27,7 +27,7 @@ {% endif %}
-

{{ unit_contact_card.title }}

+

{{ unit_contact_card.heading }}

{% if unit_contact_card.address|render %}
@@ -57,10 +57,10 @@
{% endif %} - {% if unit_contact_card.details|render %} + {% if unit_contact_card.highlights|render %}
{{ 'Additional details'|t }}:
-
{{ unit_contact_card.details }}
+
{{ unit_contact_card.highlights }}
{% endif %} From a8c461cf9fbb36626267ccb5611379acbacf03c5 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Thu, 19 Dec 2024 16:31:57 +0200 Subject: [PATCH 2/3] UHF-9507: Fix PHPCS --- hdbt.theme | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hdbt.theme b/hdbt.theme index f937ab3d5..1a4b8986e 100644 --- a/hdbt.theme +++ b/hdbt.theme @@ -942,7 +942,7 @@ function hdbt_preprocess_paragraph__unit_contact_card(array &$variables) : void $paragraph = $variables['paragraph']; $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); - // Get the unit entity and ensure it exists and has the correct translation + // Get the unit entity and ensure it exists and has the correct translation. $unit = $paragraph->get('field_unit_contact_unit') ?->first() ?->get('entity') @@ -975,7 +975,8 @@ function hdbt_preprocess_paragraph__unit_contact_card(array &$variables) : void // unit name has been overridden. if (empty($heading_value) || $heading_value === 'default') { $variables['unit_contact_card']['heading'] = t('Contact information'); - } elseif ($heading_value === 'unit_name') { + } + elseif ($heading_value === 'unit_name') { $variables['unit_contact_card']['heading'] = !empty($unit_translation?->get('name_override')->value) ? $unit_translation?->get('name_override')->view('unit_contact_card') : $unit_translation?->get('name')->view('unit_contact_card'); From f62bd2ec85264b28f1299834330bbfa6a2f60db8 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Thu, 19 Dec 2024 17:28:50 +0200 Subject: [PATCH 3/3] UHF-9507: Add translation context and translations for the unit contact card default heading --- hdbt.theme | 2 +- translations/fi.po | 4 ++++ translations/sv.po | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/hdbt.theme b/hdbt.theme index 1a4b8986e..fbc272112 100644 --- a/hdbt.theme +++ b/hdbt.theme @@ -974,7 +974,7 @@ function hdbt_preprocess_paragraph__unit_contact_card(array &$variables) : void // value Contact information. If the unit name is used check if the // unit name has been overridden. if (empty($heading_value) || $heading_value === 'default') { - $variables['unit_contact_card']['heading'] = t('Contact information'); + $variables['unit_contact_card']['heading'] = t('Contact information', [], ['context' => 'Unit contact card default heading']); } elseif ($heading_value === 'unit_name') { $variables['unit_contact_card']['heading'] = !empty($unit_translation?->get('name_override')->value) diff --git a/translations/fi.po b/translations/fi.po index c92620b70..d2564580a 100644 --- a/translations/fi.po +++ b/translations/fi.po @@ -1339,3 +1339,7 @@ msgstr "Tervetuloa hel.fin Drupalin sisällönhallintaan. Ohjeet löydät" msgctxt "User info page" msgid "Hel.fi content producer's guide (in Finnish)" msgstr "Hel.fin sisällöntuottajan oppaasta" + +msgctxt "Unit contact card default heading" +msgid "Contact information" +msgstr "Yhteystiedot" diff --git a/translations/sv.po b/translations/sv.po index d6b2aa2fe..077476098 100644 --- a/translations/sv.po +++ b/translations/sv.po @@ -1333,3 +1333,7 @@ msgstr "Välkommen till innehållshanteringen av hel.fi Drupal. Du hittar instru msgctxt "User info page" msgid "Hel.fi content producer's guide (in Finnish)" msgstr "Hel.fi innehållsproducentens guide (på finska)" + +msgctxt "Unit contact card default heading" +msgid "Contact information" +msgstr "Kontaktuppgifter"