Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
[SDPAP-8095] Added date and time conversion for news and grant conten…
Browse files Browse the repository at this point in the history
…t types. (#214)

* [SDPAP-8095] Added date and time conversion for news and grant content types.
  • Loading branch information
MdNadimHossain authored Sep 20, 2023
1 parent e22b2a0 commit a234d27
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 17 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ workflows:
branches:
ignore:
- reference
- phpunit_tests

mergetoreference:
jobs:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions src/Helper/TideLandingPageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\tide_landing_page\Helper;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
* Helper functions.
*/
class TideLandingPageHelper {

/**
* Convert given date to local time format.
*
* @param string $date
* The date element to process.
*
* @return string
* The converted date.
*/
public static function localDateAndTimeFormatter($date) {
// Parse date with GMT timezone.
$storage_tz = DateTimeItemInterface::STORAGE_TIMEZONE;
$drupal_date_time = new DrupalDateTime($date, $storage_tz);
// Convert to local timezone.
$system_tz = \Drupal::service('config.factory')->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;
}

}
37 changes: 22 additions & 15 deletions src/Plugin/jsonapi/FieldEnhancer/CardLinkEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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')) {
Expand All @@ -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()) {
Expand All @@ -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')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @group tide_landing_page
*/
class StatisticsGridCountNumericKeys extends UnitTestCase {
class StatisticsGridCountNumericKeysTest extends UnitTestCase {

/**
* Tests CountNumericKeys.
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/TideLandingPageHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Drupal\test_helpers\TestHelpers;
use Drupal\Tests\UnitTestCase;
use Drupal\tide_landing_page\Helper\TideLandingPageHelper;

/**
* Tests the TideLandingPageHelper class.
*
* @group tide_landing_page
*/
class TideLandingPageHelperTest extends UnitTestCase {

/**
* Tests the localDateAndTimeFormatter method.
*/
public function testLocalDateAndTimeFormatter() {
TestHelpers::service('date.formatter')->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);
}

}

0 comments on commit a234d27

Please sign in to comment.