From aaa5a9c0b07af3f44bbe305c869c4181b711d9d6 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Nov 2021 15:04:30 -0800 Subject: [PATCH 1/3] Update EDTF Formatter with 'no value' options Allow year and month to be hidden. Taken from Born-Digital's updates to carapace. --- .../Field/FieldFormatter/EDTFFormatter.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index ce8433e..f850124 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -63,6 +63,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { 'space' => t("Space ' '"), ], ]; + $form['date_order'] = [ '#title' => t('Date Order'), '#type' => 'select', @@ -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'), @@ -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; } @@ -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]; @@ -255,11 +272,24 @@ 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]; @@ -282,7 +312,7 @@ protected function formatDate($edtf_text) { } // Time. - // @todo Add time formatting options. + // TODO: Add time formatting options. if (array_key_exists(1, $date_time) && !empty($date_time[1])) { $formatted_date .= ' ' . $date_time[1]; } From 9583d2b51ed858ba484edd0c1118c147636a2dca Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Nov 2021 15:18:01 -0800 Subject: [PATCH 2/3] style fixes --- src/Plugin/Field/FieldFormatter/EDTFFormatter.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index f850124..337eee3 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -283,10 +283,12 @@ protected function formatDate($edtf_text) { if (array_key_exists(EDTFUtils::YEAR_BASE, $parsed_date)) { if ($settings['year_format'] == 'ny') { $year = ''; - } elseif ($settings['year_format'] = 'yy') { + } + 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); + } + else { + $year = substr(ltrim($parsed_date[EDTFUtils::YEAR_BASE], '0'), 0, 2); } } @@ -312,7 +314,7 @@ protected function formatDate($edtf_text) { } // Time. - // TODO: Add time formatting options. + // @todo: Add time formatting options. if (array_key_exists(1, $date_time) && !empty($date_time[1])) { $formatted_date .= ' ' . $date_time[1]; } From 56e73552da46b5d7fe68a9bfb6e3124d87b160a5 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Nov 2021 15:23:21 -0800 Subject: [PATCH 3/3] coding style fix --- src/Plugin/Field/FieldFormatter/EDTFFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index 337eee3..fe27802 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -314,7 +314,7 @@ protected function formatDate($edtf_text) { } // Time. - // @todo: Add time formatting options. + // @todo Add time formatting options. if (array_key_exists(1, $date_time) && !empty($date_time[1])) { $formatted_date .= ' ' . $date_time[1]; }