Skip to content

Commit 7477ee4

Browse files
authored
[6.x] Time field enhancements (#13799)
* add format_time modifier * add augment_format option to time fieldtype * pint
1 parent cd7a751 commit 7477ee4

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

lang/en/fieldtypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
'text.config.prepend' => 'Add text before (to the left of) the input.',
202202
'text.title' => 'Text',
203203
'textarea.title' => 'Textarea',
204+
'time.config.augment_format' => 'Optionally format the output using [PHP date format](https://www.php.net/manual/en/datetime.format.php) syntax. e.g. `g:ia`.',
204205
'time.config.seconds_enabled' => 'Show seconds in the timepicker.',
205206
'time.title' => 'Time',
206207
'toggle.config.inline_label' => 'Set an inline label to be shown beside the toggle input.',

src/Fieldtypes/Time.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Statamic\Fieldtypes;
44

5+
use Illuminate\Support\Facades\Date;
56
use Statamic\Fields\Fieldtype;
67
use Statamic\Rules\TimeFieldtype as ValidationRule;
78

@@ -31,11 +32,25 @@ protected function configFieldItems(): array
3132
'instructions' => __('statamic::messages.fields_default_instructions'),
3233
'type' => 'text',
3334
],
35+
'augment_format' => [
36+
'display' => __('Augment Format'),
37+
'instructions' => __('statamic::fieldtypes.time.config.augment_format'),
38+
'type' => 'text',
39+
],
3440
],
3541
],
3642
];
3743
}
3844

45+
public function augment($value)
46+
{
47+
if (! $value || ! $this->config('augment_format')) {
48+
return $value;
49+
}
50+
51+
return Date::parse($value)->format($this->config('augment_format'));
52+
}
53+
3954
public function rules(): array
4055
{
4156
return [new ValidationRule($this)];

src/Modifiers/CoreModifiers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,20 @@ public function formatTranslated($value, $params)
821821
return $this->carbon($value)->translatedFormat(Arr::get($params, 0));
822822
}
823823

824+
/**
825+
* Format a time string without timezone conversion.
826+
*
827+
* @return string
828+
*/
829+
public function formatTime($value, $params)
830+
{
831+
if (! $value) {
832+
return $value;
833+
}
834+
835+
return Date::parse($value)->format(Arr::get($params, 0, 'g:ia'));
836+
}
837+
824838
/**
825839
* Format a number with grouped thousands and decimal points.
826840
*

tests/Fieldtypes/TimeTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,53 @@ public static function validationProvider()
117117
];
118118
}
119119

120+
#[Test]
121+
#[DataProvider('augmentProvider')]
122+
public function it_augments($config, $value, $expected)
123+
{
124+
$this->assertSame($expected, $this->fieldtype($config)->augment($value));
125+
}
126+
127+
public static function augmentProvider()
128+
{
129+
return [
130+
'null without format' => [
131+
[],
132+
null,
133+
null,
134+
],
135+
'null with format' => [
136+
['augment_format' => 'g:ia'],
137+
null,
138+
null,
139+
],
140+
'time without format returns as-is' => [
141+
[],
142+
'14:30',
143+
'14:30',
144+
],
145+
'time with format' => [
146+
['augment_format' => 'g:ia'],
147+
'14:30',
148+
'2:30pm',
149+
],
150+
'time with seconds and format' => [
151+
['augment_format' => 'g:i:sa'],
152+
'14:30:45',
153+
'2:30:45pm',
154+
],
155+
];
156+
}
157+
158+
#[Test]
159+
public function it_does_not_apply_timezone_when_augmenting()
160+
{
161+
config()->set('statamic.system.display_timezone', 'Europe/Berlin');
162+
config()->set('statamic.system.localize_dates_in_modifiers', true);
163+
164+
$this->assertSame('2:30pm', $this->fieldtype(['augment_format' => 'g:ia'])->augment('14:30'));
165+
}
166+
120167
public function fieldtype($config = [])
121168
{
122169
$field = new Field('test', array_replace([

tests/Modifiers/FormatTimeTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tests\Modifiers;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Modifiers\Modify;
7+
use Tests\TestCase;
8+
9+
class FormatTimeTest extends TestCase
10+
{
11+
#[Test]
12+
public function it_formats_time_with_default_format()
13+
{
14+
$this->assertSame('2:45pm', $this->modify('14:45'));
15+
}
16+
17+
#[Test]
18+
public function it_formats_time_with_custom_format()
19+
{
20+
$this->assertSame('2:45 PM', $this->modify('14:45', 'g:i A'));
21+
}
22+
23+
#[Test]
24+
public function it_formats_time_with_seconds()
25+
{
26+
$this->assertSame('2:45:30pm', $this->modify('14:45:30', 'g:i:sa'));
27+
}
28+
29+
#[Test]
30+
public function it_does_not_apply_timezone_conversion()
31+
{
32+
config()->set('statamic.system.display_timezone', 'Europe/Berlin');
33+
config()->set('statamic.system.localize_dates_in_modifiers', true);
34+
35+
$this->assertSame('2:45pm', $this->modify('14:45', 'g:ia'));
36+
}
37+
38+
#[Test]
39+
public function it_returns_null_for_null_value()
40+
{
41+
$this->assertNull($this->modify(null, 'g:ia'));
42+
}
43+
44+
#[Test]
45+
public function it_returns_empty_string_for_empty_string_value()
46+
{
47+
$this->assertSame('', $this->modify('', 'g:ia'));
48+
}
49+
50+
public function modify($value, $format = null)
51+
{
52+
return Modify::value($value)->formatTime($format)->fetch();
53+
}
54+
}

0 commit comments

Comments
 (0)