Skip to content
Snippets Groups Projects

Feature/istwcms 7156 ebremner public facing dates

Merged Eric Bremner requested to merge feature/ISTWCMS-7156-ebremner-public-facing-dates into 1.1.x
Compare and
2 files
+ 171
1
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -3,7 +3,10 @@
namespace Drupal\uw_cfg_common\Service;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\Core\File\FileUrlGenerator;
@@ -20,6 +23,8 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
class UwNodeFieldValue {
use StringTranslationTrait;
// Date format: "Thursday, December 2, 2021".
const DATE_FORMAT_DATE_ONLY = 'l, F j, Y';
// Time format: "10:00 AM".
@@ -55,6 +60,20 @@ class UwNodeFieldValue {
*/
protected $uwService;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
/**
* Default constructor.
*
@@ -66,18 +85,26 @@ class UwNodeFieldValue {
* The file url generator.
* @param \Drupal\uw_cfg_common\Service\UWServiceInterface $uwService
* The UW Service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
* The date formatter.
*/
public function __construct(
RequestStack $requestStack,
EntityTypeManagerInterface $entityTypeManager,
FileUrlGenerator $fileUrlGenerator,
UWServiceInterface $uwService
UWServiceInterface $uwService,
LanguageManagerInterface $languageManager,
DateFormatter $dateFormatter
) {
$this->requestStack = $requestStack;
$this->entityTypeManager = $entityTypeManager;
$this->fileUrlGenerator = $fileUrlGenerator;
$this->uwService = $uwService;
$this->languageManager = $languageManager;
$this->dateFormatter = $dateFormatter;
}
/**
@@ -998,8 +1025,12 @@ class UwNodeFieldValue {
*/
public function getDates(Node $node, string $field_name, string $view_mode) {
// Have at least an empty array to return.
$return_dates = [];
// Get the current default language.
$lang_code = $this->languageManager->getDefaultLanguage()->getId();
// If there is no dates to process, return empty array.
if (!$node->$field_name->value) {
return $return_dates;
@@ -1007,6 +1038,8 @@ class UwNodeFieldValue {
// If this is not and event, just get the date.
if ($node->getType() !== 'uw_ct_event') {
// Get the dates.
$return_dates[] = $this->getDate([$node->$field_name->value], 'blog');
}
else {
@@ -1065,6 +1098,45 @@ class UwNodeFieldValue {
array_multisort($keys, SORT_ASC, $return_dates);
}
// If there are return dates and the language is not english,
// add the translations for the dates.
if (
count($return_dates) > 0 &&
$lang_code !== 'en'
) {
// Step through all the dates and get any translations
// as required.
foreach ($return_dates as $index => $date) {
// Get any dates that are present in the array.
if (isset($date['start_date'])) {
$return_dates[$index]['translated']['start_date'] = $this->getTranslatedDates(
$date['start_date'],
$lang_code,
$date['timezone'] ?? NULL
);
}
if (isset($date['end_date'])) {
$return_dates[$index]['translated']['end_date'] = $this->getTranslatedDates(
$date['end_date'],
$lang_code,
$date['timezone'] ?? NULL
);
}
if (isset($date['date'])) {
$return_dates[$index]['translated'] = $this->getTranslatedDates(
strtotime($date['date']),
$lang_code,
$date['timezone'] ?? NULL
);
}
// Add the translation for all day.
$return_dates[$index]['translated']['all_day'] = $this->t('all day');
}
}
return $return_dates;
}
@@ -1513,4 +1585,100 @@ class UwNodeFieldValue {
return $contact_emails;
}
/**
* Function to get the translated dates.
*
* @param int $date
* The timestamp of a date.
* @param string $lang_code
* The language code to translate to.
* @param string|null $timezone
* The timezone.
*
* @return array
* The array of translated dates.
*/
public function getTranslatedDates(
int $date,
string $lang_code,
string $timezone = NULL
): array {
// If no timezone supplied, set it.
if (!$timezone) {
// Set the timezone to be used.
$timezone = 'America/Toronto';
}
// Get the translations of the dates based on the language.
switch ($lang_code) {
// The Chinese date translations.
case 'zh-hans':
$translated_dates['long'] = $this->dateFormatter->format($date, 'custom', 'Y', $timezone, $lang_code);
$translated_dates['long'] .= $this->t('Year', [], [
'langcode' => $lang_code,
'context' => 'date',
])->render();
$translated_dates['long'] .= $this->dateFormatter->format($date, 'custom', 'M', $timezone, $lang_code);
$translated_dates['long'] .= $this->dateFormatter->format($date, 'custom', 'j', $timezone, $lang_code);
$translated_dates['long'] .= $this->t('Day', [], ['langcode' => $lang_code])->render();
$translated_dates['long'] .= ' ';
$translated_dates['long'] .= $this->dateFormatter->format($date, 'custom', 'l', $timezone, $lang_code);
$translated_dates['standard'] = $this->dateFormatter->format($date, 'custom', 'Y', $timezone, $lang_code);
$translated_dates['standard'] .= $this->t('Year', [], [
'langcode' => $lang_code,
'context' => 'date',
])->render();
$translated_dates['standard'] .= $this->dateFormatter->format($date, 'custom', 'j', $timezone, $lang_code);
$translated_dates['standard'] .= ' ';
$translated_dates['standard'] .= $this->dateFormatter->format($date, 'custom', 'F', $timezone, $lang_code);
$translated_dates['standard'] .= $this->t('Month', [], ['langcode' => $lang_code])->render();
$translated_dates['agenda-date'] = $this->dateFormatter->format($date, 'custom', 'n j', $timezone, $lang_code);
$translated_dates['agenda-time'] = $this->dateFormatter->format($date, 'custom', 'a g:i', $timezone, $lang_code);
$translated_dates['long_with_time'] = $this->dateFormatter->format($date, 'custom', 'Y', $timezone, $lang_code);
$translated_dates['long_with_time'] .= $this->t('Year', [], [
'langcode' => $lang_code,
'context' => 'date',
])->render();
$translated_dates['long_with_time'] .= $this->dateFormatter->format($date, 'custom', 'n', $timezone, $lang_code);
$translated_dates['long_with_time'] .= $this->t('Month', [], ['langcode' => $lang_code])->render();
$translated_dates['long_with_time'] .= $this->dateFormatter->format($date, 'custom', 'j', $timezone, $lang_code);
$translated_dates['long_with_time'] .= $this->t('Day', [], ['langcode' => $lang_code])->render() . ' ';
$translated_dates['long_with_time'] .= $this->dateFormatter->format($date, 'custom', 'l a g:i', $timezone, $lang_code);
$translated_dates['time_only'] = $this->dateFormatter->format($date, 'custom', 'a g:i', $timezone, $lang_code);
$translated_dates['timezone'] = $this->dateFormatter->format($date, 'custom', 'T (\G\M\T P)', $timezone, $lang_code);
$translated_dates['month_only'] = $this->dateFormatter->format($date, 'custom', 'Y', $timezone, $lang_code);
$translated_dates['month_only'] .= $this->t('Year', [], [
'langcode' => $lang_code,
'context' => 'date',
])->render();
$translated_dates['month_only'] .= $this->dateFormatter->format($date, 'custom', 'm', $timezone, $lang_code);
break;
// The French date translations.
case 'fr':
$translated_dates['long'] = $this->dateFormatter->format($date, 'custom', 'l j F Y', $timezone, $lang_code);
$translated_dates['standard'] = $this->dateFormatter->format($date, 'custom', 'j F Y', $timezone, $lang_code);
$translated_dates['agenda-date'] = $this->dateFormatter->format($date, 'custom', 'j F', $timezone, $lang_code);
$translated_dates['agenda-time'] = $this->dateFormatter->format($date, 'custom', 'G\hi', $timezone, $lang_code);
$translated_dates['long_with_time'] = $this->dateFormatter->format($date, 'custom', 'l j F Y G\hi', $timezone, $lang_code);
$translated_dates['time_only'] = $this->dateFormatter->format($date, 'custom', 'G\hi', $timezone, $lang_code);
$translated_dates['timezone'] = $this->dateFormatter->format($date, 'custom', 'T (\G\M\T P)', $timezone, $lang_code);
$translated_dates['month_only'] = $this->dateFormatter->format($date, 'custom', 'm Y', $timezone, $lang_code);
break;
}
// Add the translation for all day.
$translated_dates['all_day'] = $this->t('all day', [], ['langcode => $lang_code'])->render();
return $translated_dates;
}
}
Loading