Skip to content

Commit

Permalink
Merge pull request #75 from seth-shaw-unlv/seth-shaw-unlv-patch-1
Browse files Browse the repository at this point in the history
Allow year and month to be hidden when displaying EDTF dates
  • Loading branch information
rosiel authored Dec 9, 2021
2 parents 9640186 + 56e7355 commit fb69281
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Plugin/Field/FieldFormatter/EDTFFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'space' => t("Space ' '"),
],
];

$form['date_order'] = [
'#title' => t('Date Order'),
'#type' => 'select',
Expand All @@ -74,11 +75,13 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'middle_endian' => t('Middle-endian (month, day, year)'),
],
];

$form['month_format'] = [
'#title' => t('Month Format'),
'#type' => 'select',
'#default_value' => $this->getSetting('month_format'),
'#options' => [
'nm' => t('Do not show Month'),
'mm' => t('two-digit month, e.g. 04'),
'm' => t('one-digit month for months below 10, e.g. 4'),
'mmm' => t('three-letter abbreviation for month, Apr'),
Expand All @@ -90,10 +93,21 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'select',
'#default_value' => $this->getSetting('day_format'),
'#options' => [
'nd' => t('Do not show day'),
'dd' => t('two-digit day of the month, e.g. 02'),
'd' => t('one-digit day of the month for days below 10, e.g. 2'),
],
];
$form['year_format'] = [
'#title' => t('Year Format'),
'#type' => 'select',
'#default_value' => $this->getSetting('year_format'),
'#options' => [
'ny' => t('Do not show year'),
'yy' => t('two-digit day of the month, e.g. 02'),
'y' => t('four-digit representation of the year, e.g. 2020'),
],
];
return $form;
}

Expand Down Expand Up @@ -242,6 +256,9 @@ protected function formatDate($edtf_text) {
elseif ($settings['month_format'] === 'm') {
$month = ltrim($parsed_date[EDTFUtils::MONTH], ' 0');
}
elseif ($settings['month_format'] == 'nm') {
$month = "";
}
// IF 'mm', do nothing, it is already in this format.
else {
$month = $parsed_date[EDTFUtils::MONTH];
Expand All @@ -255,11 +272,26 @@ protected function formatDate($edtf_text) {
elseif ($settings['day_format'] === 'd') {
$day = ltrim($parsed_date[EDTFUtils::DAY], ' 0');
}
elseif ($settings['day_format'] == "nd") {
$day = "";
}
else {
$day = $parsed_date[EDTFUtils::DAY];
}
}

if (array_key_exists(EDTFUtils::YEAR_BASE, $parsed_date)) {
if ($settings['year_format'] == 'ny') {
$year = '';
}
elseif ($settings['year_format'] = 'yy') {
$year = ltrim($parsed_date[EDTFUtils::YEAR_BASE], '0');
}
else {
$year = substr(ltrim($parsed_date[EDTFUtils::YEAR_BASE], '0'), 0, 2);
}
}

// Put the parts back together.
if ($settings['date_order'] === 'little_endian') {
$parts_in_order = [$day, $month, $year];
Expand Down

0 comments on commit fb69281

Please sign in to comment.