From 0ab5cb02b49035645f2c4cb2c24598059aeeeee1 Mon Sep 17 00:00:00 2001 From: granitibrahimi Date: Fri, 7 Oct 2022 20:23:54 +0200 Subject: [PATCH 1/5] Update Date.php preProcessIndex to use format from config When a format is specified for a Date field and the field is present on the list, it always throw a DateTime::__construct(): Failed to parse time string exception. Example I have created a date field with the format: "y-m" and when I add that field to the list then this is the thrown exception: Could not parse '20-10': DateTime::__construct(): Failed to parse time string (20-10) at position 0 (2): Unexpected character {"userId":2,"exception":"[object] (Carbon\\Exceptions\\InvalidFormatException(code: 0): Could not parse '20-10': DateTime::__construct(): Failed to parse time string (20-10) at position 0 (2): Unexpected character at /var/www/html/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php:190) [stacktrace] #0 /var/www/html/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php(216): Carbon\\Carbon::rawParse('20-10', NULL) #1 /var/www/html/vendor/statamic/cms/src/Fieldtypes/Date.php(300): Carbon\\Carbon::parse('20-10') #2 /var/www/html/vendor/statamic/cms/src/Fieldtypes/Date.php(126): Statamic\\Fieldtypes\\Date->parseSaved('20-10') #3 /var/www/html/vendor/statamic/cms/src/Fieldtypes/Date.php(110): Statamic\\Fieldtypes\\Date->preProcessSingle('20-10') #4 /var/www/html/vendor/statamic/cms/src/Fields/Field.php(314): Statamic\\Fieldtypes\\Date->preProcess('20-10') #5 /var/www/html/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php(60): Statamic\\Fields\\Field->preProcess() #6 [internal function]: Illuminate\\Support\\HigherOrderCollectionProxy->Illuminate\\Support\\{closure}(Object(Statamic\\Fields\\Field), 'date --- src/Fieldtypes/Date.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index 515235ff76..836d631a38 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -196,6 +196,10 @@ public function preProcessIndex($data) return $start.' - '.$end; } + + if(!is_null($this->config('format'))){ + return Carbon::createFromFormat($this->config('format'), $data)->format($this->indexDisplayFormat()); + } return Carbon::parse($data)->format($this->indexDisplayFormat()); } From aed3b5647517a11b57566c81c50da58106ff1147 Mon Sep 17 00:00:00 2001 From: granitibrahimi Date: Fri, 7 Oct 2022 20:25:57 +0200 Subject: [PATCH 2/5] Adjust the condition for the config check --- src/Fieldtypes/Date.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index 836d631a38..af72ffb8bc 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -197,11 +197,11 @@ public function preProcessIndex($data) return $start.' - '.$end; } - if(!is_null($this->config('format'))){ - return Carbon::createFromFormat($this->config('format'), $data)->format($this->indexDisplayFormat()); + if(is_null($this->config('format'))){ + return Carbon::parse($data)->format($this->indexDisplayFormat()); } - return Carbon::parse($data)->format($this->indexDisplayFormat()); + return Carbon::createFromFormat($this->config('format'), $data)->format($this->indexDisplayFormat()); } private function saveFormat() From 43f0f9b95fe856a4d9c7e187fbded321c9bf6b58 Mon Sep 17 00:00:00 2001 From: granitibrahimi Date: Fri, 7 Oct 2022 20:55:38 +0200 Subject: [PATCH 3/5] Fixed StyleCI issues --- src/Fieldtypes/Date.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index af72ffb8bc..6e2d9debd5 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -197,7 +197,7 @@ public function preProcessIndex($data) return $start.' - '.$end; } - if(is_null($this->config('format'))){ + if (is_null($this->config('format'))) { return Carbon::parse($data)->format($this->indexDisplayFormat()); } From 03376453ba7b25ee5fe2267b366b4a3f76274bf8 Mon Sep 17 00:00:00 2001 From: granitibrahimi Date: Fri, 7 Oct 2022 21:01:05 +0200 Subject: [PATCH 4/5] Fixed StyleCI issues --- src/Fieldtypes/Date.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index 6e2d9debd5..f3bfcb54b2 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -196,7 +196,7 @@ public function preProcessIndex($data) return $start.' - '.$end; } - + if (is_null($this->config('format'))) { return Carbon::parse($data)->format($this->indexDisplayFormat()); } From efc5ad3716ef6cd5f989fa193827ca3b91440338 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 10 Oct 2022 14:20:32 -0400 Subject: [PATCH 5/5] use existing method --- src/Fieldtypes/Date.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index f3bfcb54b2..3611adc227 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -197,11 +197,7 @@ public function preProcessIndex($data) return $start.' - '.$end; } - if (is_null($this->config('format'))) { - return Carbon::parse($data)->format($this->indexDisplayFormat()); - } - - return Carbon::createFromFormat($this->config('format'), $data)->format($this->indexDisplayFormat()); + return $this->parseSaved($data)->format($this->indexDisplayFormat()); } private function saveFormat()