Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow year and month to be hidden when displaying EDTF dates #75

Merged
merged 3 commits into from
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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