From a234d2709a1697ca2a6f8d10e3ac8382f6910e3b Mon Sep 17 00:00:00 2001 From: Md Nadim Hossain Date: Wed, 20 Sep 2023 10:04:43 +1000 Subject: [PATCH] [SDPAP-8095] Added date and time conversion for news and grant content types. (#214) * [SDPAP-8095] Added date and time conversion for news and grant content types. --- .circleci/config.yml | 1 + composer.json | 3 +- src/Helper/TideLandingPageHelper.php | 33 +++++++++++++++++ .../FieldEnhancer/CardLinkEnhancer.php | 37 +++++++++++-------- ...=> StatisticsGridCountNumericKeysTest.php} | 2 +- tests/unit/TideLandingPageHelperTest.php | 28 ++++++++++++++ 6 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 src/Helper/TideLandingPageHelper.php rename tests/unit/{StatisticsGridCountNumericKeys.php => StatisticsGridCountNumericKeysTest.php} (91%) create mode 100644 tests/unit/TideLandingPageHelperTest.php diff --git a/.circleci/config.yml b/.circleci/config.yml index c2a9f06e..af8498c1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -122,6 +122,7 @@ workflows: branches: ignore: - reference + - phpunit_tests mergetoreference: jobs: diff --git a/composer.json b/composer.json index e436095c..617930a0 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "dpc-sdp/tide_core": "^3.0.1", "dpc-sdp/tide_event": "^3.0.0", - "dpc-sdp/tide_media": "^3.0.9" + "dpc-sdp/tide_media": "^3.0.9", + "dpc-sdp/tide_api": "^3.0.14" }, "suggest": { "dpc-sdp/tide_api:^3.0.8": "Allows to use Drupal in headless mode", diff --git a/src/Helper/TideLandingPageHelper.php b/src/Helper/TideLandingPageHelper.php new file mode 100644 index 00000000..24e46ee0 --- /dev/null +++ b/src/Helper/TideLandingPageHelper.php @@ -0,0 +1,33 @@ +get('system.date')->get('timezone.default'); + $date_formatter = \Drupal::service('date.formatter'); + $converted_date = $date_formatter->format($drupal_date_time->getTimeStamp(), 'custom', 'Y-m-d H:i:s', $system_tz); + return $converted_date; + } + +} diff --git a/src/Plugin/jsonapi/FieldEnhancer/CardLinkEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/CardLinkEnhancer.php index 5380abda..f0a6b258 100644 --- a/src/Plugin/jsonapi/FieldEnhancer/CardLinkEnhancer.php +++ b/src/Plugin/jsonapi/FieldEnhancer/CardLinkEnhancer.php @@ -2,14 +2,13 @@ namespace Drupal\tide_landing_page\Plugin\jsonapi\FieldEnhancer; -use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\EntityInterface; -use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\file\Entity\File; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\media\Entity\Media; use Drupal\paragraphs\Entity\Paragraph; use Drupal\taxonomy\Entity\Term; +use Drupal\tide_landing_page\Helper\TideLandingPageHelper; use Drupal\tide_media\Plugin\jsonapi\FieldEnhancer\ImageEnhancer; use Shaper\Util\Context; @@ -124,7 +123,13 @@ public function getCardFields(EntityInterface $node) { if ($module_handler->moduleExists('tide_news')) { // Add the date field for news. if ($node->hasField('field_news_date') && !$node->field_news_date->isEmpty()) { - $card_fields['date'] = $node->get('field_news_date')->getValue()[0]; + $start_date = $node->get('field_news_date')->getValue()[0]['value'] ?? NULL; + $end_date = $node->get('field_news_date')->getValue()[0]['end_value'] ?? NULL; + $news_dates = [ + 'value' => TideLandingPageHelper::localDateAndTimeFormatter($start_date), + 'end_value' => TideLandingPageHelper::localDateAndTimeFormatter($end_date), + ]; + $card_fields['date'] = $news_dates ? $news_dates : ''; } } if ($module_handler->moduleExists('tide_event')) { @@ -136,25 +141,27 @@ public function getCardFields(EntityInterface $node) { $event_date = $paragraph->field_paragraph_date_range->getValue()[0]; // Parse date with GMT timezone. if ($event_date != NULL) { - $storage_tz = DateTimeItemInterface::STORAGE_TIMEZONE; - $date_value = new DrupalDateTime($event_date['value'], $storage_tz); - $date_end_value = new DrupalDateTime($event_date['end_value'], $storage_tz); - $system_tz = \Drupal::service('config.factory')->get('system.date')->get('timezone.default'); - // Convert to local timezone. - $date_formatter = \Drupal::service('date.formatter'); - $event_date = [ - 'value' => $date_formatter->format($date_value->getTimeStamp(), 'custom', 'Y-m-d H:i:s', $system_tz), - 'end_value' => $date_formatter->format($date_end_value->getTimeStamp(), 'custom', 'Y-m-d H:i:s', $system_tz), + $start_date = $event_date['value'] ?? NULL; + $end_date = $event_date['end_value'] ?? NULL; + $event_dates = [ + 'value' => TideLandingPageHelper::localDateAndTimeFormatter($start_date), + 'end_value' => TideLandingPageHelper::localDateAndTimeFormatter($end_date), ]; + $card_fields['date'] = $event_dates ? $event_dates : ''; } - $card_fields['date'] = $event_date ? $event_date : ''; } } } if ($module_handler->moduleExists('tide_grant')) { // Add the date field for grants. if ($node->hasField('field_node_dates') && !$node->field_node_dates->isEmpty()) { - $card_fields['date'] = $node->get('field_node_dates')->getValue()[0]; + $start_date = $node->get('field_node_dates')->getValue()[0]['value'] ?? NULL; + $end_date = $node->get('field_node_dates')->getValue()[0]['end_value'] ?? NULL; + $grants_dates = [ + 'value' => TideLandingPageHelper::localDateAndTimeFormatter($start_date), + 'end_value' => TideLandingPageHelper::localDateAndTimeFormatter($end_date), + ]; + $card_fields['date'] = $grants_dates ? $grants_dates : ''; } // Add the ongoing field for grants. if ($node->hasField('field_node_on_going') && !$node->field_node_on_going->isEmpty()) { @@ -176,7 +183,7 @@ public function getCardFields(EntityInterface $node) { } // Add the date field for publication. if ($node->hasField('field_publication_date') && !$node->field_publication_date->isEmpty()) { - $card_fields['date'] = $node->get('field_publication_date')->getValue()[0]; + $card_fields['date'] = TideLandingPageHelper::localDateAndTimeFormatter($node->get('field_publication_date')->getValue()[0]); } } if ($module_handler->moduleExists('tide_profile')) { diff --git a/tests/unit/StatisticsGridCountNumericKeys.php b/tests/unit/StatisticsGridCountNumericKeysTest.php similarity index 91% rename from tests/unit/StatisticsGridCountNumericKeys.php rename to tests/unit/StatisticsGridCountNumericKeysTest.php index 7cd3771f..e64f0570 100644 --- a/tests/unit/StatisticsGridCountNumericKeys.php +++ b/tests/unit/StatisticsGridCountNumericKeysTest.php @@ -10,7 +10,7 @@ * * @group tide_landing_page */ -class StatisticsGridCountNumericKeys extends UnitTestCase { +class StatisticsGridCountNumericKeysTest extends UnitTestCase { /** * Tests CountNumericKeys. diff --git a/tests/unit/TideLandingPageHelperTest.php b/tests/unit/TideLandingPageHelperTest.php new file mode 100644 index 00000000..9e82eb79 --- /dev/null +++ b/tests/unit/TideLandingPageHelperTest.php @@ -0,0 +1,28 @@ +stubSetFormat('medium', 'Medium', 'd.m.Y'); + TestHelpers::service('config.factory')->stubSetConfig('system.date', ['timezone.default' => 'Australia/Melbourne']); + // Instantiate the TideLandingPageHelper class with the mocked objects. + $helper = new TideLandingPageHelper(); + // Call the method to be tested. + $result = $helper->localDateAndTimeFormatter('2023-09-29T13:59:00'); + // Assert that the result matches the expected output. + $this->assertEquals('2023-09-29 23:59:00', $result); + } + +}