Skip to content

Commit fb69281

Browse files
authored
Merge pull request #75 from seth-shaw-unlv/seth-shaw-unlv-patch-1
Allow year and month to be hidden when displaying EDTF dates
2 parents 9640186 + 56e7355 commit fb69281

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Plugin/Field/FieldFormatter/EDTFFormatter.php

+32
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
6363
'space' => t("Space ' '"),
6464
],
6565
];
66+
6667
$form['date_order'] = [
6768
'#title' => t('Date Order'),
6869
'#type' => 'select',
@@ -74,11 +75,13 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
7475
'middle_endian' => t('Middle-endian (month, day, year)'),
7576
],
7677
];
78+
7779
$form['month_format'] = [
7880
'#title' => t('Month Format'),
7981
'#type' => 'select',
8082
'#default_value' => $this->getSetting('month_format'),
8183
'#options' => [
84+
'nm' => t('Do not show Month'),
8285
'mm' => t('two-digit month, e.g. 04'),
8386
'm' => t('one-digit month for months below 10, e.g. 4'),
8487
'mmm' => t('three-letter abbreviation for month, Apr'),
@@ -90,10 +93,21 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
9093
'#type' => 'select',
9194
'#default_value' => $this->getSetting('day_format'),
9295
'#options' => [
96+
'nd' => t('Do not show day'),
9397
'dd' => t('two-digit day of the month, e.g. 02'),
9498
'd' => t('one-digit day of the month for days below 10, e.g. 2'),
9599
],
96100
];
101+
$form['year_format'] = [
102+
'#title' => t('Year Format'),
103+
'#type' => 'select',
104+
'#default_value' => $this->getSetting('year_format'),
105+
'#options' => [
106+
'ny' => t('Do not show year'),
107+
'yy' => t('two-digit day of the month, e.g. 02'),
108+
'y' => t('four-digit representation of the year, e.g. 2020'),
109+
],
110+
];
97111
return $form;
98112
}
99113

@@ -242,6 +256,9 @@ protected function formatDate($edtf_text) {
242256
elseif ($settings['month_format'] === 'm') {
243257
$month = ltrim($parsed_date[EDTFUtils::MONTH], ' 0');
244258
}
259+
elseif ($settings['month_format'] == 'nm') {
260+
$month = "";
261+
}
245262
// IF 'mm', do nothing, it is already in this format.
246263
else {
247264
$month = $parsed_date[EDTFUtils::MONTH];
@@ -255,11 +272,26 @@ protected function formatDate($edtf_text) {
255272
elseif ($settings['day_format'] === 'd') {
256273
$day = ltrim($parsed_date[EDTFUtils::DAY], ' 0');
257274
}
275+
elseif ($settings['day_format'] == "nd") {
276+
$day = "";
277+
}
258278
else {
259279
$day = $parsed_date[EDTFUtils::DAY];
260280
}
261281
}
262282

283+
if (array_key_exists(EDTFUtils::YEAR_BASE, $parsed_date)) {
284+
if ($settings['year_format'] == 'ny') {
285+
$year = '';
286+
}
287+
elseif ($settings['year_format'] = 'yy') {
288+
$year = ltrim($parsed_date[EDTFUtils::YEAR_BASE], '0');
289+
}
290+
else {
291+
$year = substr(ltrim($parsed_date[EDTFUtils::YEAR_BASE], '0'), 0, 2);
292+
}
293+
}
294+
263295
// Put the parts back together.
264296
if ($settings['date_order'] === 'little_endian') {
265297
$parts_in_order = [$day, $month, $year];

0 commit comments

Comments
 (0)