Skip to content

Commit d25d155

Browse files
[TASK] Document \Datetime as extbase model type (#5565)
* [TASK] Document \Datetime as extbase model type Releases: main, 13.4 * [TASK] Language checks Releases: main * [TASK] Updated code snippet paths Releases: main --------- Co-authored-by: Sarah McCarthy <[email protected]>
1 parent f84dddc commit d25d155

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

Documentation/ExtensionArchitecture/Extbase/Reference/Domain/Model/PropertyTypes/Index.rst

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,46 @@ In TYPO3 v13, boolean values are typically managed using
200200
.. literalinclude:: _codesnippets/_bool_example.php
201201
:caption: packages/my_extension/Configuration/TCA/tx_myextension_domain_model_boolexample.php
202202

203+
.. _extbase-model-predefined-classes:
204+
205+
Predefined classes as types of models
206+
=====================================
207+
208+
.. _extbase-model-datetime:
209+
210+
Datetime model types
211+
--------------------
212+
213+
The PHP classes :php:`\DateTime` and :php:`\DateTimeImmutable` can be used with
214+
the TCA field type `datetime <https://docs.typo3.org/permalink/t3tca:columns-datetime>`_
215+
216+
The value can be stored in the database as either a unix timestamp :sql:`int(11)`
217+
(default) or type :sql:`datetime` (TCA :ref:`dbType <t3tca:confval-datetime-dbtype>`
218+
`datetime`).
219+
220+
In the frontend they are commonly displayed using the `f:format.date
221+
ViewHelper <f:format.date> <https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-format-date>`_.
222+
223+
.. tabs::
224+
225+
.. group-tab:: Model
226+
227+
.. literalinclude:: _codesnippets/_DateExample.php
228+
:caption: packages/my_extension/Classes/Domain/Model/DateExample.php
229+
230+
Use :php:`\DateTimeImmutable` if you want the date to be
231+
immutable.
232+
233+
.. group-tab:: TCA
234+
235+
.. literalinclude:: _codesnippets/_date_example.php
236+
:caption: packages/my_extension/Configuration/TCA/tx_myextension_domain_model_dateexample.php
237+
238+
.. group-tab:: Fluid
239+
240+
.. literalinclude:: _codesnippets/_Date.html
241+
:caption: packages/my_extension/Resources/Private/Templates/Date/Show.html
242+
203243
.. _extbase-model-enumerations:
204244

205245
Enumerations as Extbase model property
@@ -211,8 +251,7 @@ Enumerations as Extbase model property
211251
has been introduced. It is no longer necessary to extend the deprecated
212252
TYPO3 Core class :ref:`\\TYPO3\\CMS\\Core\\Type\\Enumeration <Enumerations-How-to-use>`.
213253

214-
215-
Native PHP enumerations can be used for properties, if a database field has a
254+
Native PHP enumerations can be used for properties where the database field has a
216255
set of values which can be represented by a backed enum. A property
217256
with an enum type should be used with a TCA field that only allows specific
218257
values to be stored in the database, for example `Select fields <https://docs.typo3.org/permalink/t3tca:columns-select>`_
@@ -247,7 +286,7 @@ and `Radio buttons <https://docs.typo3.org/permalink/t3tca:columns-radio>`_.
247286

248287
.. group-tab:: Fluid
249288

250-
.. literalinclude:: _codesnippets/_enum_example.php
289+
.. literalinclude:: _codesnippets/_EnumExample.html
251290
:caption: packages/my_extension/Resources/Private/Templates/Paper/Show.html
252291

253292
.. group-tab:: Localization
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<f:format.date format="%d. %B %Y">{example.datetimeInt}</f:format.date>
2+
<f:format.date format="%d. %B %Y">{example.datetimeDatetime}</f:format.date>
3+
4+
Or inline:
5+
6+
{example.datetimeInt -> f:format.date(format: '%d. %B %Y')}
7+
{example.datetimeDatetime -> f:format.date(format: '%d. %B %Y')}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MyVendor\MyExtension\Domain\Model;
6+
7+
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
8+
9+
class DateExample extends AbstractEntity
10+
{
11+
/**
12+
* A datetime stored in an integer field
13+
*/
14+
public ?\DateTime $datetimeInt = null;
15+
16+
/**
17+
* A datetime stored in a datetime field
18+
*/
19+
public ?\DateTime $datetimeDatetime = null;
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
// ...
7+
'columns' => [
8+
'datetime_text' => [
9+
'exclude' => true,
10+
'label' => 'type=datetime, db=text',
11+
'config' => [
12+
'type' => 'datetime',
13+
],
14+
],
15+
'datetime_int' => [
16+
'exclude' => true,
17+
'label' => 'type=datetime, db=int',
18+
'config' => [
19+
'type' => 'datetime',
20+
],
21+
],
22+
'datetime_datetime' => [
23+
'exclude' => true,
24+
'label' => 'type=datetime, db=datetime',
25+
'config' => [
26+
'type' => 'datetime',
27+
'dbType' => 'datetime',
28+
'nullable' => true,
29+
],
30+
],
31+
],
32+
];

0 commit comments

Comments
 (0)