From a19aef9ba94d759e42dd3a642886502ee2960ed0 Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Fri, 20 Sep 2024 10:23:07 -0300 Subject: [PATCH 1/6] updated unspecified date output --- .../Field/FieldFormatter/EDTFFormatter.php | 109 +++++++++++++----- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index aa4a249..0695184 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -294,6 +294,28 @@ protected function formatDate($edtf_text) { } } + // replace Xs with 0s and format date parts + if (count($unspecified) > 0) { + if (in_array(t('year'), $unspecified)) { + if ($year === 'XXXX') + $year = t('unknown year'); + else + $year = t('unknown year in the decade of the @dates', ['@date' => str_replace('X', '0', $year)]); + } + elseif (in_array(t('decade'), $unspecified)) { + $year = t('unknown year in the century of the @dates', ['@date' => str_replace('X', '0', $year)]); + } + elseif (in_array(t('century'), $unspecified)) { + $year = t('unknown year in the millennium of the @dates', ['@date' => str_replace('X', '0', $year)]); + } + if (in_array(t('month'), $unspecified)) { + $month = t('unknown month'); + } + if (in_array(t('day'), $unspecified)) { + $day = t('unknown day'); + } + } + // Put the parts back together. if ($settings['date_order'] === 'little_endian') { $parts_in_order = [$day, $month, $year]; @@ -306,51 +328,76 @@ protected function formatDate($edtf_text) { $parts_in_order = [$year, $month, $day]; } - // Special case for dates such as "Dec 29, 2021". + // Special cases for middle endian dates separated by spaces, with months spelled out + // Full dates will have a comma before the year, like January 1, 1999 + // Dates with Xs in them will be written out more verbosely + $d = intval($day); + $day_suffix = date('S',mktime(1,1,1,1,( (($d>=10)+($d>=20)+($d==0))*10 + $d%10) )); if ($settings['date_order'] === 'middle_endian' && !preg_match('/\d/', $month) && self::DELIMITERS[$settings['date_separator']] == ' ' && - count(array_filter([$year, $day])) == 2) { - $formatted_date = "$month $day, $year"; + count(array_filter([$month, $day])) > 0) { + // unknown year only + if (!in_array(t('day'), $unspecified) && !in_array(t('month'), $unspecified) && count($unspecified) === 1) { + $formatted_date = t(trim("$month $day") . ", of an $year"); + } + // unknown month only + elseif(in_array(t('month'), $unspecified) && count($unspecified) === 1) { + if ($day !== '') + $day .= "$day_suffix day of an"; + $formatted_date = t(trim("$day $month, in $year")); + } + // unknown day only + elseif(in_array(t('day'), $unspecified) && count($unspecified) === 1) { + $formatted_date = t("$day in $month, $year"); + } + // unknown year and month only + elseif(!in_array(t('day'), $unspecified) && count($unspecified) === 2) { + if ($day !== '') + $day .= "$day_suffix day of an"; + if ($year == t('unknown year')) + $formatted_date = t("$day $month, in an $year"); + else + $formatted_date = t(trim("$day $month, in the " . str_replace('unknown year in the ', '', $year))); + } + // unknown year and day only + elseif(!in_array(t('month'), $unspecified) && count($unspecified) === 2) { + if ($year == t('unknown year')) + $formatted_date = t("$day in $month, in an $year"); + else + $formatted_date = t("$day in $month, in the " . str_replace('unknown year in the ', '', $year)); + } + // unknown day and month only + elseif(!in_array(t('year'), $unspecified) && count($unspecified) === 2) { + $formatted_date = t("Unknown date, in $year"); + } + // unknown year, month, and day + elseif(count($unspecified) === 3) { + if ($year == t('unknown year')) + $formatted_date = t("Unknown day, month, and year"); + else + $formatted_date = t("Unknown date, in the " . str_replace('unknown year in the ', '', $year)); + } + // no unknown segments + // Adds a comma after the month & day as long as there is at least one of them + else { + $formatted_date = trim("$month $day") . ", $year"; + } } else { $formatted_date = implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order)); } + // capitalize first letter for unknown dates + if (count($unspecified) > 0) + $formatted_date = ucfirst($formatted_date); + // Time. // @todo Add time formatting options. if (array_key_exists(1, $date_time) && !empty($date_time[1])) { $formatted_date .= ' ' . $date_time[1]; } - // Unspecified. - // Year = 1, Month = 2, Day = 4. - switch (count($unspecified)) { - case 1: - $formatted_date = t('unspecified @time_unit in @date', [ - '@time_unit' => $unspecified[0], - '@date' => $formatted_date, - ]); - break; - - case 2: - $formatted_date = t('unspecified @time_unit1 and @time_unit2 in @date', [ - '@time_unit1' => $unspecified[0], - '@time_unit2' => $unspecified[1], - '@date' => $formatted_date, - ]); - break; - - case 3: - $formatted_date = t('unspecified @time_unit1, @time_unit2, and @time_unit3 in @date', [ - '@time_unit1' => $unspecified[0], - '@time_unit2' => $unspecified[1], - '@time_unit3' => $unspecified[2], - '@date' => $formatted_date, - ]); - break; - } - // Qualified. // This is ugly and terrible, but I'm out of ideas for simplifying it. $qualifiers = [ From 4e444f45190fb3ef6a82c60c405ffbb58e51c103 Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Thu, 26 Sep 2024 10:59:31 -0300 Subject: [PATCH 2/6] code cleanup. removed extra t() calls --- .../Field/FieldFormatter/EDTFFormatter.php | 111 ++++++++++-------- 1 file changed, 64 insertions(+), 47 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index 0695184..683fc5e 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -231,26 +231,39 @@ protected function formatDate($edtf_text) { } // Unspecified. - $unspecified = []; + $unspecified = array( + 'fullyear' => false, + 'year' => false, + 'century' => false, + 'decade' => false, + 'month' => false, + 'day' => false, + ); + $unspecified_count = 0; + if (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XXXX') !== FALSE) { - $unspecified[] = t('year'); + $unspecified['fullyear'] = true; + $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XXX') !== FALSE) { - $unspecified[] = t('century'); + $unspecified['century'] = true; + $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XX') !== FALSE) { - $unspecified[] = t('decade'); + $unspecified['decade'] = true; + $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'X') !== FALSE) { - $unspecified[] = t('year'); + $unspecified['year'] = true; + $unspecified_count++; } - // Clean-up unspecified year/decade. - $year = str_replace('X', '0', $parsed_date[EDTFUtils::YEAR_BASE]); + + $year = $parsed_date[EDTFUtils::YEAR_BASE]; if (array_key_exists(EDTFUtils::MONTH, $parsed_date)) { if (strpos($parsed_date[EDTFUtils::MONTH], 'X') !== FALSE) { - $unspecified[] = t('month'); - // Month remains blank for output. + $unspecified['month'] = true; + $unspecified_count++; } elseif ($settings['month_format'] === 'mmm' || $settings['month_format'] === 'mmmm') { $month = EDTFUtils::MONTHS_MAP[$parsed_date[EDTFUtils::MONTH]][$settings['month_format']]; @@ -269,7 +282,8 @@ protected function formatDate($edtf_text) { if (array_key_exists(EDTFUtils::DAY, $parsed_date)) { if (strpos($parsed_date[EDTFUtils::DAY], 'X') !== FALSE) { - $unspecified[] = t('day'); + $unspecified['day'] = true; + $unspecified_count++; } elseif ($settings['day_format'] === 'd') { $day = ltrim($parsed_date[EDTFUtils::DAY], ' 0'); @@ -294,25 +308,28 @@ protected function formatDate($edtf_text) { } } - // replace Xs with 0s and format date parts - if (count($unspecified) > 0) { - if (in_array(t('year'), $unspecified)) { - if ($year === 'XXXX') - $year = t('unknown year'); - else - $year = t('unknown year in the decade of the @dates', ['@date' => str_replace('X', '0', $year)]); + // Replace Xs with 0s and format date parts + if ($unspecified_count > 0) { + if (strpos($year, 'X') !== FALSE) + $year = str_replace('X', '0', $year) . 's'; + + if ($unspecified['fullyear']) { + $year = 'unknown year'; + } + elseif ($unspecified['year']) { + $year = "unknown year in the decade of the $year"; } - elseif (in_array(t('decade'), $unspecified)) { - $year = t('unknown year in the century of the @dates', ['@date' => str_replace('X', '0', $year)]); + elseif ($unspecified['decade']) { + $year = "unknown year in the century of the $year"; } - elseif (in_array(t('century'), $unspecified)) { - $year = t('unknown year in the millennium of the @dates', ['@date' => str_replace('X', '0', $year)]); + elseif ($unspecified['century']) { + $year = "unknown year in the millennium of the $year"; } - if (in_array(t('month'), $unspecified)) { - $month = t('unknown month'); + if ($unspecified['month']) { + $month = 'unknown month'; } - if (in_array(t('day'), $unspecified)) { - $day = t('unknown day'); + if ($unspecified['day']) { + $day = 'unknown day'; } } @@ -337,59 +354,59 @@ protected function formatDate($edtf_text) { !preg_match('/\d/', $month) && self::DELIMITERS[$settings['date_separator']] == ' ' && count(array_filter([$month, $day])) > 0) { - // unknown year only - if (!in_array(t('day'), $unspecified) && !in_array(t('month'), $unspecified) && count($unspecified) === 1) { + // Unknown year only + if (!$unspecified['day'] && !$unspecified['month'] && $unspecified_count === 1) { $formatted_date = t(trim("$month $day") . ", of an $year"); } - // unknown month only - elseif(in_array(t('month'), $unspecified) && count($unspecified) === 1) { + // Unknown month only + elseif($unspecified['month'] && $unspecified_count === 1) { if ($day !== '') $day .= "$day_suffix day of an"; $formatted_date = t(trim("$day $month, in $year")); } - // unknown day only - elseif(in_array(t('day'), $unspecified) && count($unspecified) === 1) { + // Unknown day only + elseif($unspecified['day'] && $unspecified_count === 1) { $formatted_date = t("$day in $month, $year"); } - // unknown year and month only - elseif(!in_array(t('day'), $unspecified) && count($unspecified) === 2) { + // Unknown year and month only + elseif(!$unspecified['day'] && $unspecified_count === 2) { if ($day !== '') $day .= "$day_suffix day of an"; - if ($year == t('unknown year')) + if ($year == 'unknown year') $formatted_date = t("$day $month, in an $year"); else $formatted_date = t(trim("$day $month, in the " . str_replace('unknown year in the ', '', $year))); } - // unknown year and day only - elseif(!in_array(t('month'), $unspecified) && count($unspecified) === 2) { - if ($year == t('unknown year')) + // Unknown year and day only + elseif(!$unspecified['month'] && $unspecified_count === 2) { + if ($year == 'unknown year') $formatted_date = t("$day in $month, in an $year"); else $formatted_date = t("$day in $month, in the " . str_replace('unknown year in the ', '', $year)); } - // unknown day and month only - elseif(!in_array(t('year'), $unspecified) && count($unspecified) === 2) { + // Unknown day and month only + elseif($unspecified['day'] && $unspecified['month'] && $unspecified_count === 2) { $formatted_date = t("Unknown date, in $year"); } - // unknown year, month, and day - elseif(count($unspecified) === 3) { - if ($year == t('unknown year')) + // Unknown year, month, and day + elseif($unspecified_count === 3) { + if ($year == 'unknown year') $formatted_date = t("Unknown day, month, and year"); else $formatted_date = t("Unknown date, in the " . str_replace('unknown year in the ', '', $year)); } - // no unknown segments + // No unknown segments // Adds a comma after the month & day as long as there is at least one of them else { - $formatted_date = trim("$month $day") . ", $year"; + $formatted_date = t(trim("$month $day") . ", $year"); } } else { - $formatted_date = implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order)); + $formatted_date = t(implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order))); } - // capitalize first letter for unknown dates - if (count($unspecified) > 0) + // Capitalize first letter for unknown dates + if ($unspecified_count > 0) $formatted_date = ucfirst($formatted_date); // Time. From 892a1e9d654a5cbf3b45bbabdde5bab18df9471c Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Thu, 26 Sep 2024 11:11:59 -0300 Subject: [PATCH 3/6] code cleanup --- .../Field/FieldFormatter/EDTFFormatter.php | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index 683fc5e..e5fcf34 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -310,8 +310,9 @@ protected function formatDate($edtf_text) { // Replace Xs with 0s and format date parts if ($unspecified_count > 0) { - if (strpos($year, 'X') !== FALSE) + if (strpos($year, 'X') !== FALSE) { $year = str_replace('X', '0', $year) . 's'; + } if ($unspecified['fullyear']) { $year = 'unknown year'; @@ -349,7 +350,7 @@ protected function formatDate($edtf_text) { // Full dates will have a comma before the year, like January 1, 1999 // Dates with Xs in them will be written out more verbosely $d = intval($day); - $day_suffix = date('S',mktime(1,1,1,1,( (($d>=10)+($d>=20)+($d==0))*10 + $d%10) )); + $day_suffix = date('S',mktime(1, 1, 1, 1, ((($d >= 10) + ($d >= 20) + ($d == 0)) * 10 + $d % 10))); if ($settings['date_order'] === 'middle_endian' && !preg_match('/\d/', $month) && self::DELIMITERS[$settings['date_separator']] == ' ' && @@ -360,8 +361,9 @@ protected function formatDate($edtf_text) { } // Unknown month only elseif($unspecified['month'] && $unspecified_count === 1) { - if ($day !== '') + if ($day !== '') { $day .= "$day_suffix day of an"; + } $formatted_date = t(trim("$day $month, in $year")); } // Unknown day only @@ -370,19 +372,24 @@ protected function formatDate($edtf_text) { } // Unknown year and month only elseif(!$unspecified['day'] && $unspecified_count === 2) { - if ($day !== '') + if ($day !== '') { $day .= "$day_suffix day of an"; - if ($year == 'unknown year') + } + if ($year == 'unknown year') { $formatted_date = t("$day $month, in an $year"); - else + } + else { $formatted_date = t(trim("$day $month, in the " . str_replace('unknown year in the ', '', $year))); + } } // Unknown year and day only elseif(!$unspecified['month'] && $unspecified_count === 2) { - if ($year == 'unknown year') + if ($year == 'unknown year') { $formatted_date = t("$day in $month, in an $year"); - else + } + else { $formatted_date = t("$day in $month, in the " . str_replace('unknown year in the ', '', $year)); + } } // Unknown day and month only elseif($unspecified['day'] && $unspecified['month'] && $unspecified_count === 2) { @@ -390,10 +397,12 @@ protected function formatDate($edtf_text) { } // Unknown year, month, and day elseif($unspecified_count === 3) { - if ($year == 'unknown year') + if ($year == 'unknown year') { $formatted_date = t("Unknown day, month, and year"); - else + } + else { $formatted_date = t("Unknown date, in the " . str_replace('unknown year in the ', '', $year)); + } } // No unknown segments // Adds a comma after the month & day as long as there is at least one of them @@ -406,8 +415,9 @@ protected function formatDate($edtf_text) { } // Capitalize first letter for unknown dates - if ($unspecified_count > 0) + if ($unspecified_count > 0) { $formatted_date = ucfirst($formatted_date); + } // Time. // @todo Add time formatting options. From 37879d131093327584328ac29078b7b7e50b3083 Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Thu, 26 Sep 2024 12:24:49 -0300 Subject: [PATCH 4/6] code cleanup --- .../Field/FieldFormatter/EDTFFormatter.php | 124 +++++++++++------- 1 file changed, 78 insertions(+), 46 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index e5fcf34..aab654e 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -231,30 +231,30 @@ protected function formatDate($edtf_text) { } // Unspecified. - $unspecified = array( - 'fullyear' => false, - 'year' => false, - 'century' => false, - 'decade' => false, - 'month' => false, - 'day' => false, - ); + $unspecified = [ + 'fullyear' => FALSE, + 'year' => FALSE, + 'century' => FALSE, + 'decade' => FALSE, + 'month' => FALSE, + 'day' => FALSE, + ]; $unspecified_count = 0; if (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XXXX') !== FALSE) { - $unspecified['fullyear'] = true; + $unspecified['fullyear'] = TRUE; $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XXX') !== FALSE) { - $unspecified['century'] = true; + $unspecified['century'] = TRUE; $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'XX') !== FALSE) { - $unspecified['decade'] = true; + $unspecified['decade'] = TRUE; $unspecified_count++; } elseif (strpos($parsed_date[EDTFUtils::YEAR_BASE], 'X') !== FALSE) { - $unspecified['year'] = true; + $unspecified['year'] = TRUE; $unspecified_count++; } @@ -262,7 +262,7 @@ protected function formatDate($edtf_text) { if (array_key_exists(EDTFUtils::MONTH, $parsed_date)) { if (strpos($parsed_date[EDTFUtils::MONTH], 'X') !== FALSE) { - $unspecified['month'] = true; + $unspecified['month'] = TRUE; $unspecified_count++; } elseif ($settings['month_format'] === 'mmm' || $settings['month_format'] === 'mmmm') { @@ -282,7 +282,7 @@ protected function formatDate($edtf_text) { if (array_key_exists(EDTFUtils::DAY, $parsed_date)) { if (strpos($parsed_date[EDTFUtils::DAY], 'X') !== FALSE) { - $unspecified['day'] = true; + $unspecified['day'] = TRUE; $unspecified_count++; } elseif ($settings['day_format'] === 'd') { @@ -308,7 +308,7 @@ protected function formatDate($edtf_text) { } } - // Replace Xs with 0s and format date parts + // Replace Xs with 0s and format date parts. if ($unspecified_count > 0) { if (strpos($year, 'X') !== FALSE) { $year = str_replace('X', '0', $year) . 's'; @@ -346,75 +346,107 @@ protected function formatDate($edtf_text) { $parts_in_order = [$year, $month, $day]; } - // Special cases for middle endian dates separated by spaces, with months spelled out - // Full dates will have a comma before the year, like January 1, 1999 - // Dates with Xs in them will be written out more verbosely + // Special cases for middle endian dates separated by spaces, with months spelled out. + // Full dates will have a comma before the year, like January 1, 1999. + // Dates with Xs in them will be written out more verbosely. $d = intval($day); $day_suffix = date('S',mktime(1, 1, 1, 1, ((($d >= 10) + ($d >= 20) + ($d == 0)) * 10 + $d % 10))); if ($settings['date_order'] === 'middle_endian' && !preg_match('/\d/', $month) && self::DELIMITERS[$settings['date_separator']] == ' ' && count(array_filter([$month, $day])) > 0) { - // Unknown year only + // Unknown year only. if (!$unspecified['day'] && !$unspecified['month'] && $unspecified_count === 1) { - $formatted_date = t(trim("$month $day") . ", of an $year"); + $formatted_date = t("@md, of an @year", [ + "@md" => trim("$month $day"), + "@year" => $year, + ]); } - // Unknown month only - elseif($unspecified['month'] && $unspecified_count === 1) { + // Unknown month only. + elseif ($unspecified['month'] && $unspecified_count === 1) { if ($day !== '') { $day .= "$day_suffix day of an"; } - $formatted_date = t(trim("$day $month, in $year")); - } - // Unknown day only - elseif($unspecified['day'] && $unspecified_count === 1) { - $formatted_date = t("$day in $month, $year"); - } - // Unknown year and month only - elseif(!$unspecified['day'] && $unspecified_count === 2) { + $formatted_date = t("@dm, in @year", [ + "@dm" => trim("$day $month"), + "@year" => $year, + ]); + } + // Unknown day only. + elseif ($unspecified['day'] && $unspecified_count === 1) { + $formatted_date = t("@day in @month, @year", [ + "@day" => $day, + "@month" => $month, + "@year" => $year, + ]); + } + // Unknown year and month only. + elseif (!$unspecified['day'] && $unspecified_count === 2) { if ($day !== '') { $day .= "$day_suffix day of an"; } if ($year == 'unknown year') { - $formatted_date = t("$day $month, in an $year"); + $formatted_date = t("@day @month, in an @year", [ + "@day" => $day, + "@month" => $month, + "@year" => $year, + ]); } else { - $formatted_date = t(trim("$day $month, in the " . str_replace('unknown year in the ', '', $year))); + $formatted_date = t("@dm, in the @year", [ + "@dm" => trim("$day $month"), + "@year" => str_replace('unknown year in the ', '', $year), + ]); } } - // Unknown year and day only - elseif(!$unspecified['month'] && $unspecified_count === 2) { + // Unknown year and day only. + elseif (!$unspecified['month'] && $unspecified_count === 2) { if ($year == 'unknown year') { - $formatted_date = t("$day in $month, in an $year"); + $formatted_date = t("@day in @month, in an @year", [ + "@day" => $day, + "@month" => $month, + "@year" => $year, + ]); } else { - $formatted_date = t("$day in $month, in the " . str_replace('unknown year in the ', '', $year)); + $formatted_date = t("@day in @month, in the @year", [ + "@day" => $day, + "@month" => $month, + "@year" => str_replace('unknown year in the ', '', $year), + ]); } } - // Unknown day and month only - elseif($unspecified['day'] && $unspecified['month'] && $unspecified_count === 2) { - $formatted_date = t("Unknown date, in $year"); + // Unknown day and month only. + elseif ($unspecified['day'] && $unspecified['month'] && $unspecified_count === 2) { + $formatted_date = t("Unknown date, in @year", [ + "@year" => $year, + ]); } - // Unknown year, month, and day - elseif($unspecified_count === 3) { + // Unknown year, month, and day. + elseif ($unspecified_count === 3) { if ($year == 'unknown year') { $formatted_date = t("Unknown day, month, and year"); } else { - $formatted_date = t("Unknown date, in the " . str_replace('unknown year in the ', '', $year)); + $formatted_date = t("Unknown date, in the @year", [ + "@year" => str_replace('unknown year in the ', '', $year), + ]); } } - // No unknown segments - // Adds a comma after the month & day as long as there is at least one of them + // No unknown segments. + // Adds a comma after the month & day as long as there is at least one of them. else { - $formatted_date = t(trim("$month $day") . ", $year"); + $formatted_date = t("@md, @year", [ + "@md" => trim("$month $day"), + "@year" => $year, + ]); } } else { $formatted_date = t(implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order))); } - // Capitalize first letter for unknown dates + // Capitalize first letter for unknown dates. if ($unspecified_count > 0) { $formatted_date = ucfirst($formatted_date); } From 3843936874b82527b9da5f4fd50eb4510af4a065 Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Thu, 26 Sep 2024 13:39:26 -0300 Subject: [PATCH 5/6] more code styling fixes --- .../Field/FieldFormatter/EDTFFormatter.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php index aab654e..118117b 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -346,11 +346,11 @@ protected function formatDate($edtf_text) { $parts_in_order = [$year, $month, $day]; } - // Special cases for middle endian dates separated by spaces, with months spelled out. + // Special cases for middle endian dates with spaces and months spelled out. // Full dates will have a comma before the year, like January 1, 1999. // Dates with Xs in them will be written out more verbosely. $d = intval($day); - $day_suffix = date('S',mktime(1, 1, 1, 1, ((($d >= 10) + ($d >= 20) + ($d == 0)) * 10 + $d % 10))); + $day_suffix = date('S', mktime(1, 1, 1, 1, ((($d >= 10) + ($d >= 20) + ($d == 0)) * 10 + $d % 10))); if ($settings['date_order'] === 'middle_endian' && !preg_match('/\d/', $month) && self::DELIMITERS[$settings['date_separator']] == ' ' && @@ -387,10 +387,10 @@ protected function formatDate($edtf_text) { } if ($year == 'unknown year') { $formatted_date = t("@day @month, in an @year", [ - "@day" => $day, - "@month" => $month, - "@year" => $year, - ]); + "@day" => $day, + "@month" => $month, + "@year" => $year, + ]); } else { $formatted_date = t("@dm, in the @year", [ @@ -434,7 +434,7 @@ protected function formatDate($edtf_text) { } } // No unknown segments. - // Adds a comma after the month & day as long as there is at least one of them. + // Adds a comma after the month & day. else { $formatted_date = t("@md, @year", [ "@md" => trim("$month $day"), @@ -443,7 +443,9 @@ protected function formatDate($edtf_text) { } } else { - $formatted_date = t(implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order))); + $formatted_date = t("@date", [ + "@date" => implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order)) + ]); } // Capitalize first letter for unknown dates. From d924b14c13fa1968d22a0d655514ce5796bdc49a Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Thu, 26 Sep 2024 13:43:17 -0300 Subject: [PATCH 6/6] hopefully the final code 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 118117b..5a11327 100644 --- a/src/Plugin/Field/FieldFormatter/EDTFFormatter.php +++ b/src/Plugin/Field/FieldFormatter/EDTFFormatter.php @@ -444,7 +444,7 @@ protected function formatDate($edtf_text) { } else { $formatted_date = t("@date", [ - "@date" => implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order)) + "@date" => implode(self::DELIMITERS[$settings['date_separator']], array_filter($parts_in_order)), ]); }