#104589TCA generation for domain models by php attributes
Description
This feature provides an alternative way of generating the TCA for domain models based on php attributes. It does not replace the TCA as underlying structure, but makes files in EXT:my_extension/Configuration/TCA/ obsolete in most cases.
Benefits
- field configuration is placed directly next to the property definition in the model – no local separation of related information
- autocompletion of attribute properties in IDE
- reduces duplicated code by providing useful default values
- can be used in combination with classic TCA files
- https://github.com/phantasie-schmiede/psb-foundation/tree/main/Classes/Attribute/TCA
- https://github.com/phantasie-schmiede/psb-foundation/blob/main/Classes/Service/Configuration/TcaService.php (method buildFromAttributes)
That extension already provides a collection of attributes that covers many of the available TCA options (but not yet 100%). The default values for each property have been derived from many TCA files in some projects to cover most use cases and reduce the number of custom values passed to the attribute constructor.
Examples
Classic TCA file
[
'ctrl' => [
'title' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_myclass',
'label' => 'uid',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'delete' => 'deleted',
'default_sortby' => 'uid',
'iconfile' => 'EXT:core/Resources/Public/Icons/T3Icons/svgs/content/content-extension.svg',
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
'endtime' => 'endtime',
],
'transOrigPointerField' => 'l18n_parent',
'transOrigDiffSourceField' => 'l18n_diffsource',
'languageField' => 'sys_language_uid',
'translationSource' => 'l10n_source',
],
'columns' => [
'title' => [
'label' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_myclass.title',
'config' => [
'type' => 'input',
'size' => 40,
'max' => 255,
'eval' => 'trim'
],
],
'description' => [
'label' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_myclass.description',
'config' => [
'type' => 'text',
'cols' => 32,
'rows' => 5,
'eval' => 'trim'
],
],
'description' => [
'label' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_myclass.number',
'config' => [
'type' => 'number',
'format' => 'integer',
],
],
],
]
New attribute approach
#[Ctrl]
class MyClass
{
#[Input]
protected string $title = '';
#[Text]
protected string $description = '';
#[Number]
protected int $value = 0;
}
Default values can be overridden by constructor arguments:
#[Ctrl(label: 'title', searchFields: ['title', 'description'])]
class MyClass
{
#[Input(size: 20)]
protected string $title = '';
#[Text(enableRichText: true)]
protected string $description = '';
#[Number]
protected int $value = 0;
}
The directory Classes/Domain/Model/ of all active packages is scanned for all classes (skipping abstract ones) that have a Ctrl attribute attached. It is checked if a model relates to an existing table in the database and detects, if it extends another model from a different extension and manipulates the TCA accordingly. When extending another domain model, the default values of the Ctrl attribute are not taken into account, but only those explicitly passed to the constructor to avoid unwanted changes to the original TCA.
Properties without TCA attribute will not be considered in TCA generation. Some configurations will be added automatically if specific fields are defined in the Ctrl attribute (which they are by default): e. g. enableColumns and language fields.
The attributes are not simply transformed into arrays, but have concrete getters that enable additional logic and the implementation of events that focus on small parts of the TCA – it is not necessary to always pass and process the entire TCA array. If a property is null, it will not be included in the final TCA. This prevents the TCA from becoming unnecessarily bloated with unused options.
Example of an auxiliary property
#[Select(linkedModel: MyRelatedModel::class)]
protected MyRelatedModel $relatedModel = null;
linkedModel is resolved to foreign_table during TCA generation. Additionally, properties like foreign_field and mm_opposite_field accept property names. These will be converted to column names.
If a scenario could not be covered by the attributes, the generated TCA can be extended and modified in the usual way with TCA files in established locations. The loading order during TCA generation would be as follows:
- TCA attributes
- Configuration/TCA/
- Configuration/TCA/Overrides/
As the final TCA is cached, the more complex process only has a minimal impact on performance.
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 4
-
Benni Mack commented and changed status
NewNeeds Feedback <span data-processor="relativetime" data-value="2024-10-31T13:36:45+00:00" > 10/31/24, 13:36 </span> -
Daniel Ablass commented <span data-processor="relativetime" data-value="2024-11-22T12:45:47+00:00" > 11/22/24, 12:45 </span>
Benni Mack wrote in #note-1:
> Hey Daniel,
>
> thanks for opening up this feature request.
>
> How would you deal with the rendering of the fields in FormEngine?Hi Benni,
I didn’t thought about FormEngine yet. The current prototype doesn’t change anything regarding this, but simply translates the attributes to the TCA array form, which is then processed as usual. That retains the possibility to change the generated TCA via TCA/Overrides/.
-
Christian Kuhn commented and changed status
Needs FeedbackClosed priorityShould haveWon't have this time <span data-processor="relativetime" data-value="2025-06-04T16:23:27+00:00" > 06/04/25, 16:23 </span>Hey.
If we decide to remove PHP TCA array files in general, this needs a well thought through plan. I see many open questions:
- TCA is extensible easily. Having a layer in front, creating the array from it, to then use TcaSchema (which is new in v13) does not fit too well together. Attributes should be final and probably wouldn’t be easily extensible. load order questions and “which extension extends what and when” are well thought through in current TCA, work quite well and are absolutely battle proven. All this fully pops up again if we invent a new approach.
- We’d kill a massive ecosystem if we try to get rid of TCA array. this is the most impactful 20 year old corner stone of the core. Nearly every single extension out there uses it.
- It will be a lot of work with not that much benefit except maybe better hinting and I wonder if that work wouldn’t be better suited elsewhere. It will mid-term force every single extension to adapt, it will need very well thought through deprecation and mitigation layers and a very solid test base. And we probably don’t want to have another “you could alternatively do this instead” solution for such an important core area, either.
I think single agencies can very well create their own “build my TCA” extension, and i know there are various solutions in the wild. I currently see only very little chance to get a fully thought through approach going. As such, I’ll close the issue for now as “won’t happen anytime soon”. For content elements there is also the content_blocks extension which relies on yaml and solves related things. A massive amount of work has been put into it and even this extension did not fully make it into core (at least at the time of this writing). We could potentially pick something up again and discuss approaches, but I think we too far away to justify an open forge issue about this.
-
Daniel Ablass commented <span data-processor="relativetime" data-value="2025-06-06T11:09:41+00:00" > 06/06/25, 11:09 </span>
Hey Christian,
thanks for your detailed explanation on why this feature won’t make it anytime soon into the core. I understand your arguments and agree with you.
Hey Daniel,
thanks for opening up this feature request.
How would you deal with the rendering of the fields in FormEngine?