diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index ce8433e..fe27802 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,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];