julienmonnerie/kirby/config/fields/mixins/datetime.php

36 lines
665 B
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
use Kirby\Toolkit\Date;
return [
2022-08-31 15:02:43 +02:00
'props' => [
/**
* Defines a custom format that is used when the field is saved
*/
2025-04-21 18:57:21 +02:00
'format' => function (string|null $format = null) {
2022-08-31 15:02:43 +02:00
return $format;
}
],
'methods' => [
'toDatetime' => function ($value, string $format = 'Y-m-d H:i:s') {
if ($date = Date::optional($value)) {
if ($this->step) {
$step = Date::stepConfig($this->step);
$date->round($step['unit'], $step['size']);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $date->format($format);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return null;
}
],
'save' => function ($value) {
if ($date = Date::optional($value)) {
return $date->format($this->format);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return '';
},
2022-06-17 17:51:59 +02:00
];