From 2a74cd0058b07b49ed87b694300da3e89ecadab5 Mon Sep 17 00:00:00 2001 From: Sergei Pavlenko Date: Mon, 18 Jan 2021 22:05:00 +0200 Subject: [PATCH] EWPP-664: Remove some code duplications. --- .../oe_theme_content_call_proposals.module | 25 ++++----- .../oe_theme_content_call_tenders.module | 21 +++---- .../oe_theme_content_entity_contact.module | 17 ++---- .../oe_theme_content_event.module | 55 ++++--------------- .../oe_theme_content_news.module | 15 ++--- .../oe_theme_content_project.module | 26 ++++----- .../oe_theme_content_publication.module | 30 ++++------ .../oe_theme_helper/oe_theme_helper.module | 22 ++++++++ 8 files changed, 80 insertions(+), 131 deletions(-) diff --git a/modules/oe_theme_content_call_proposals/oe_theme_content_call_proposals.module b/modules/oe_theme_content_call_proposals/oe_theme_content_call_proposals.module index b506e7322..77a6ee13d 100644 --- a/modules/oe_theme_content_call_proposals/oe_theme_content_call_proposals.module +++ b/modules/oe_theme_content_call_proposals/oe_theme_content_call_proposals.module @@ -37,25 +37,20 @@ function oe_theme_content_call_proposals_node_view_alter(array &$build, EntityIn // Field labels are different in frontend and backend. // Here we override them, in full and teaser view mode. $label_overrides = []; - if (in_array($build['#view_mode'], ['full', 'teaser']) - && ($entity->get('oe_call_proposals_deadline')->count() > 1)) { + if ($entity->get('oe_call_proposals_deadline')->count() > 1) { $label_overrides['oe_call_proposals_deadline'] = t('Deadline dates'); } - if ($build['#view_mode'] === 'full') { - $label_overrides += [ - 'oe_departments' => t('Departments'), - 'oe_call_proposals_grants' => t('Results'), - ]; + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full', 'teaser']); - if ($entity->get('oe_departments')->count() === 1) { - $label_overrides['oe_departments'] = t('Department'); - } - } + $label_overrides += [ + 'oe_departments' => t('Departments'), + 'oe_call_proposals_grants' => t('Results'), + ]; - foreach ($label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } + if ($entity->get('oe_departments')->count() === 1) { + $label_overrides['oe_departments'] = t('Department'); } + + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); } diff --git a/modules/oe_theme_content_call_tenders/oe_theme_content_call_tenders.module b/modules/oe_theme_content_call_tenders/oe_theme_content_call_tenders.module index 2c5b28220..0b8ee8f0f 100644 --- a/modules/oe_theme_content_call_tenders/oe_theme_content_call_tenders.module +++ b/modules/oe_theme_content_call_tenders/oe_theme_content_call_tenders.module @@ -22,22 +22,15 @@ function oe_theme_content_call_tenders_node_view_alter(array &$build, EntityInte // Field labels are different in frontend and backend. // Here we override them, in full and teaser view mode. - if (in_array($build['#view_mode'], ['full', 'teaser'])) { - $label_overrides = [ - 'body' => t('Description'), - 'oe_departments' => t('Departments'), - ]; + $label_overrides = [ + 'body' => t('Description'), + 'oe_departments' => t('Departments'), + ]; - if ($entity->get('oe_departments')->count() === 1) { - $label_overrides['oe_departments'] = t('Department'); - } - - foreach ($label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } + if ($entity->get('oe_departments')->count() === 1) { + $label_overrides['oe_departments'] = t('Department'); } + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full', 'teaser']); } /** diff --git a/modules/oe_theme_content_entity_contact/oe_theme_content_entity_contact.module b/modules/oe_theme_content_entity_contact/oe_theme_content_entity_contact.module index d78e76058..24f3421ca 100644 --- a/modules/oe_theme_content_entity_contact/oe_theme_content_entity_contact.module +++ b/modules/oe_theme_content_entity_contact/oe_theme_content_entity_contact.module @@ -16,16 +16,9 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; function oe_theme_content_entity_contact_oe_contact_view_alter(array &$build, ContactInterface $entity, EntityViewDisplayInterface $display) { // Field labels are different in frontend and backend. // Here we override them, per view mode. - if ($build['#view_mode'] === 'full') { - $label_overrides = [ - 'oe_address' => t('Postal address'), - 'oe_social_media' => t('Social media'), - ]; - - foreach ($label_overrides as $name => $label_override) { - if ($entity->hasField($name) && !$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } - } + $label_overrides = [ + 'oe_address' => t('Postal address'), + 'oe_social_media' => t('Social media'), + ]; + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); } diff --git a/modules/oe_theme_content_event/oe_theme_content_event.module b/modules/oe_theme_content_event/oe_theme_content_event.module index e3ffdc09c..f8131c8c6 100644 --- a/modules/oe_theme_content_event/oe_theme_content_event.module +++ b/modules/oe_theme_content_event/oe_theme_content_event.module @@ -11,34 +11,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\oe_theme\ValueObject\DateValueObject; -/** - * Implements hook_theme(). - */ -function oe_theme_content_event_theme() { - return [ - 'oe_theme_content_event_registration_button' => [ - 'variables' => [ - 'label' => '', - 'url' => '', - 'description' => '', - 'enabled' => FALSE, - 'show_button' => TRUE, - ], - ], - 'oe_theme_content_event_summary' => [ - 'variables' => [ - 'text' => '', - ], - ], - 'oe_theme_content_event_contacts' => [ - 'variables' => [ - 'general' => [], - 'press' => [], - ], - ], - ]; -} - /** * Implements hook_ENTITY_TYPE_view_alter(). */ @@ -50,23 +22,16 @@ function oe_theme_content_event_node_view_alter(array &$build, EntityInterface $ // Field labels are different in frontend and backend. // Here we override them, per view mode. - if ($build['#view_mode'] === 'full') { - $label_overrides = [ - 'oe_event_dates' => t('When'), - 'oe_event_online_type' => t('Live stream'), - 'oe_event_languages' => t('Languages'), - 'oe_event_website' => t('Website'), - 'oe_event_registration_capacity' => t('Number of seats'), - 'oe_event_entrance_fee' => t('Entrance fee'), - 'oe_event_venue' => t('Where'), - ]; - - foreach ($label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } - } + $label_overrides = [ + 'oe_event_dates' => t('When'), + 'oe_event_online_type' => t('Live stream'), + 'oe_event_languages' => t('Languages'), + 'oe_event_website' => t('Website'), + 'oe_event_registration_capacity' => t('Number of seats'), + 'oe_event_entrance_fee' => t('Entrance fee'), + 'oe_event_venue' => t('Where'), + ]; + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); } /** diff --git a/modules/oe_theme_content_news/oe_theme_content_news.module b/modules/oe_theme_content_news/oe_theme_content_news.module index 93e31f084..c58b6d37c 100644 --- a/modules/oe_theme_content_news/oe_theme_content_news.module +++ b/modules/oe_theme_content_news/oe_theme_content_news.module @@ -41,16 +41,9 @@ function oe_theme_content_news_node_view_alter(array &$build, EntityInterface $e return; } - if ($build['#view_mode'] === 'full') { - // Rename field labels based on number of items. - $plural_label_overrides = [ - 'oe_author' => t('Authors'), - ]; - - foreach ($plural_label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && $entity->get($name)->count() > 1 && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } + $label_overrides = []; + if ($entity->get('oe_author')->count() > 1) { + $label_overrides['oe_author'] = t('Authors'); } + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); } diff --git a/modules/oe_theme_content_project/oe_theme_content_project.module b/modules/oe_theme_content_project/oe_theme_content_project.module index fa9ddadbf..e50609fa8 100644 --- a/modules/oe_theme_content_project/oe_theme_content_project.module +++ b/modules/oe_theme_content_project/oe_theme_content_project.module @@ -21,22 +21,16 @@ function oe_theme_content_project_node_view_alter(array &$build, EntityInterface // Field labels are different in frontend and backend. // Here we override them, per view mode. - if ($build['#view_mode'] === 'full') { - $label_overrides = [ - 'oe_project_dates' => t('Project duration'), - 'oe_project_website' => t('Project website'), - ]; + $label_overrides = [ + 'oe_project_dates' => t('Project duration'), + 'oe_project_website' => t('Project website'), + ]; - // If there is only a start date, set "Start date" as label. - $dates = $entity->get('oe_project_dates')->getValue(); - if (!empty($dates[0]) && (empty($dates[0]['end_value']) || $dates[0]['value'] === $dates[0]['end_value'])) { - $label_overrides['oe_project_dates'] = t('Start date'); - } - - foreach ($label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } + // If there is only a start date, set "Start date" as label. + $dates = $entity->get('oe_project_dates')->getValue(); + if (!empty($dates[0]) && (empty($dates[0]['end_value']) || $dates[0]['value'] === $dates[0]['end_value'])) { + $label_overrides['oe_project_dates'] = t('Start date'); } + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); + } diff --git a/modules/oe_theme_content_publication/oe_theme_content_publication.module b/modules/oe_theme_content_publication/oe_theme_content_publication.module index 2076e4570..0f82b67a0 100644 --- a/modules/oe_theme_content_publication/oe_theme_content_publication.module +++ b/modules/oe_theme_content_publication/oe_theme_content_publication.module @@ -34,23 +34,17 @@ function oe_theme_content_publication_node_view_alter(array &$build, EntityInter } // Override field labels. - if ($build['#view_mode'] === 'full') { - $label_overrides = [ - 'oe_reference_codes' => t('Identification'), - ]; - - if ($entity->get('oe_departments')->count() > 1) { - $label_overrides['oe_departments'] = t('Related departments'); - } - - if ($entity->get('oe_publication_countries')->count() > 1) { - $label_overrides['oe_publication_countries'] = t('Countries'); - } - - foreach ($label_overrides as $name => $label_override) { - if (!$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { - $build[$name]['#title'] = $label_override; - } - } + $label_overrides = [ + 'oe_reference_codes' => t('Identification'), + ]; + + if ($entity->get('oe_departments')->count() > 1) { + $label_overrides['oe_departments'] = t('Related departments'); } + + if ($entity->get('oe_publication_countries')->count() > 1) { + $label_overrides['oe_publication_countries'] = t('Countries'); + } + _oe_theme_helper_field_view_label_override($build, $entity, $label_overrides, ['full']); + } diff --git a/modules/oe_theme_helper/oe_theme_helper.module b/modules/oe_theme_helper/oe_theme_helper.module index 092776c55..e5cfea40b 100644 --- a/modules/oe_theme_helper/oe_theme_helper.module +++ b/modules/oe_theme_helper/oe_theme_helper.module @@ -210,3 +210,25 @@ function template_preprocess_oe_theme_helper_call_status(array &$variables) { function oe_theme_helper_locale_translation_projects_alter(&$projects) { $projects['oe_theme_helper']['info']['interface translation server pattern'] = drupal_get_path('module', 'oe_theme_helper') . '/translations/%project-%language.po'; } + +/** + * Helper function for overriding field labels before rendering. + * + * @param $build + * The renderable array. + * @param EntityInterface $entity + * The entity. + * @param $label_overrides + * Array with overridden labels keyed by field name. + * @param $display_modes + * Array with display modes. + */ +function _oe_theme_helper_field_view_label_override(&$build, EntityInterface $entity, $label_overrides, $display_modes): void { + if (in_array($build['#view_mode'], $display_modes)) { + foreach ($label_overrides as $name => $label_override) { + if ($entity->hasField($name) && !$entity->get($name)->isEmpty() && isset($build[$name]['#title'])) { + $build[$name]['#title'] = $label_override; + } + } + } +}