Documentation

Create TCA files of table

Or copy link

Create TCA files of table

Estimated reading: minutes 3 views

3 views
Min read

1. TCA file’s name should be <table_name>.php and it will resides in in employee/Configuration/TCA/ folder

2. There will be one TCA file per database table.

3. Here in this example TCA file path are: i ) employee/Configuration/TCA/tx_employee_domain_model_employ.php and ii ) employee/Configuration/TCA/tx_employee_domain_model_education.php

4. The Table Configuration Array (or $GLOBALS[‘TCA’], TCA) is a global array in TYPO3 which extends the definition of database tables beyond what can be done strictly with SQL.

5. First and foremost $GLOBALS[‘TCA’] defines which tables are editable in the TYPO3 backend.

6. Database tables with no corresponding entry in $GLOBALS[‘TCA’] are “invisible” to the TYPO3 backend, so if no TCA file is created for individual table, then you will not find any option in backend to add new record for that particular table.

7. the $GLOBALS[‘TCA’] definition of a table also covers the following:

  1. Relations between that table and other tables.
  2. What fields should be displayed in the backend and with which layout.
  3. How should a field be validated (e.g. required, integer, etc.).
  4. How a field is used in the frontend by Extbase and any extension that may refer to this information.

8. The general structure for a TCA file (looking at a single table) is as follows:
(The code of TCA files are included after this point)

1. [‘ctrl’] The table

1.1. Reference for ctrl : https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/Ctrl/Index.html#ctrl 

1.2. These are basically divided in two main categories:

1.2.1. properties which affect how the table is displayed and handled in the backend interface . ( This includes which icon, what name, which columns contains the title value, which column defines the type value etc.)

1.2.2. properties which determine how it is processed by the system (TCE). (This includes publishing control, “deleted” flag, whether the table can only be edited by admin-users, may only exist in the tree root etc.)
Employee TCA file for “ctrl” (file path: employee/Configuration/TCA/tx_employee_domain_model_employ.php)

Education TCA file for “ctrl” (file path: employee/Configuration/TCA/tx_employee_domain_model_education.php)

2. [‘interface’] Backend interface handling

2.1. The “interface” section contains properties related to the tables display in the backend, mostly the Web > List module.

2.2. Ref. link: https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/Interface/Index.html#interface

3. [‘columns’] Individual fields

3.1. The “columns” section contains configuration for each table field (also called “column”) which can be edited in the backend.

3.2. Ref. link : https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/Columns/Index.html#columns 
Employee TCA file

Education TCA file

4. [‘types’] Form layout for editing

4.1. The “types” section defines how the fields in the table (configured in the “columns” section) should be arranged inside the editing form; in which order, with which “palettes” (see below) and with which possible additional features applied.

4.2. Ref. link : https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/Types/Index.html#types
Employee TCA file

Education TCA file

5. [‘palettes’] Palette fields order

5.1. A palette is just a list of fields which will be arranged horizontally side-by-side.

5.2. Ref. link : https://docs.typo3.org/m/typo3/reference-tca/12.4/en-us/Palettes/Index.html#palettes

Employ table TCA full code

Path: employee/Configuration/TCA/tx_employee_domain_model_employ.php

<?php
return [
    'ctrl' => [
        'title' => 'Employe',
        'label' => 'first_name',
        'tstamp' => 'tstamp',
        'crdate' => 'crdate',
        'versioningWS' => true,
        'label_alt_force' => true,
        'origUid' => 't3_origuid',
        'languageField' => 'sys_language_uid',
        'transOrigPointerField' => 'l10n_parent',
        'transOrigDiffSourceField' => 'l10n_diffsource',
        'delete' => 'deleted',
        'enablecolumns' => [
            'disabled' => 'hidden',
            'starttime' => 'starttime',
            'endtime' => 'endtime',
        ],
        'searchFields' => 'first_name,last_name,bio',
        'iconfile' => 'EXT:employee/Resources/Public/Icons/Extension.png',
        'security' => [
            'ignorePageTypeRestriction' => true,
        ],
    ],
    'types' => [
        '1' => [
            'showitem' => 'first_name,last_name,gender,birth_date,joining_date,image,bio,experiance,salary,languages,country, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language, sys_language_uid, l10n_parent, l10n_diffsource, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access, hidden, starttime, endtime'
        ],
    ],
    'columns' => [
        'sys_language_uid' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.language',
            'config' => [
                'type' => 'language',
            ],
        ],
        'l10n_parent' => [
            'displayCond' => 'FIELD:sys_language_uid:>:0',
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.l18n_parent',
            'config' => [
                'type' => 'select',
                'renderType' => 'selectSingle',
                'default' => 0,
                'items' => [
                    ['label' => '', 'value' => 0]
                ],
                'foreign_table' => 'tx_employee_domain_model_employ',
                'foreign_table_where' => 'AND {#tx_employee_domain_model_employ}.{#pid}=###CURRENT_PID### AND {#tx_employee_domain_model_employ}.{#sys_language_uid} IN (-1,0)',
            ],
        ],
        'l10n_diffsource' => [
            'config' => [
                'type' => 'passthrough',
            ],
        ],
        'hidden' => [
            'config' => [
                'type' => 'check',
                'items' => [
                    ['label' => 'Disable'],
                ],
            ]
        ],
        'starttime' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.starttime',
            'config' => [
                'type' => 'input',
                'renderType' => 'datetime',
                'eval' => 'datetime,int',
                'default' => 0,
                'behaviour' => [
                    'allowLanguageSynchronization' => true
                ]
            ],
        ],
        'endtime' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.endtime',
            'config' => [
                'type' => 'input',
                'renderType' => 'datetime',
                'eval' => 'datetime,int',
                'default' => 0,
                'range' => [
                    'upper' => mktime(0, 0, 0, 1, 1, 2038)
                ],
                'behaviour' => [
                    'allowLanguageSynchronization' => true
                ]
            ],
        ],
        'first_name' => [
            'l10n_mode' => 'prefixLangTitle',
            'label' => 'First Name',
            'description' => 'Enter First Name',
            'config' => [
                'type' => 'input',
                'behaviour' => [
                    'allowLanguageSynchronization' => true,
                ],
            ]
        ],
        'last_name' => [
            'l10n_mode' => 'prefixLangTitle',
            'label' => 'Last Name',
            'description' => 'Enter Last Name',
            'config' => [
                'type' => 'input',
                'behaviour' => [
                    'allowLanguageSynchronization' => true,
                ],
            ]
        ],
        'gender' => [
            'label' => 'Gender',
            'description' => 'Select Gender',
            'config' => [
                'type' => 'radio',
                'items' => [
                    [
                        'label' => 'Male',
                        'value' => 1,
                    ],
                    [
                        'label' => 'FeMale',
                        'value' => 2,
                    ]
                ],
            ],
        ],
        'birth_date' => [
            'label' => 'Birth Date',
            'config' => [
                'type' => 'datetime',
                'format' => 'date',
                'eval' => 'int',
                'default' => 0,
            ]
        ],
        'joining_date' => [
            'label' => 'Joining Date',
            'config' => [
                'type' => 'datetime',
                'format' => 'datetime',
                'eval' => 'int',
                'default' => 0,
            ]
        ],
        'image' => [
            'label' => 'Image',
            'config' => [
                'type' => 'file',
                'allowed' => 'common-image-types'
            ],
        ],
        "bio" => [
            'label' => 'Bio',
            'config' => [
                'type' => 'text',
                'cols' => 20,
                'rows' => 2,
            ],
        ],
        "experiance" => [
            'label' => 'Experiance',
            'config' => [
                'type' => 'text',
                'enableRichtext' => true,
            ],
        ],
        "salary" => [
            'label' => 'Salary',
            'config' => [
                'type' => 'number',
                'format' => 'decimal'
            ]
        ],
        "languages" => [
            'label' => 'Languages',
            'config' => [
                'type' => 'check',
                'items' => [
                    [
                        'label' => 'English',
                        'value' => 'en',
                    ],
                    [
                        'label' => 'German',
                        'value' => 'de',
                    ],
                    [
                        'label' => 'French',
                        'value' => 'fr',
                    ],
                ],
            ],
        ],
        "country" => [
            'label' => 'Country',
            'config' => [
                'type' => 'select',
                'renderType' => 'selectSingle',
                'items' => [
                     [
                        'label' => 'US',
                        'value' => 'US',
                    ],
                    [
                        'label' => 'Germany',
                        'value' => 'Germany',
                    ],
                    [
                        'label' => 'France',
                        'value' => 'France',
                    ],
                ],
            ],
        ]
    ],
];

Education table TCA full code

Path: employee/Configuration/TCA/tx_employee_domain_model_education.php

<?php
return [
    'ctrl' => [
        'title' => 'Education',
        'label' => 'title',
        'tstamp' => 'tstamp',
        'crdate' => 'crdate',
        'versioningWS' => true,
        'label_alt_force' => true,
        'origUid' => 't3_origuid',
        'languageField' => 'sys_language_uid',
        'transOrigPointerField' => 'l10n_parent',
        'transOrigDiffSourceField' => 'l10n_diffsource',
        'delete' => 'deleted',
        'enablecolumns' => [
            'disabled' => 'hidden',
            'starttime' => 'starttime',
            'endtime' => 'endtime',
        ],
        'searchFields' => 'title,rollnumber,university',
        'iconfile' => 'EXT:employee/Resources/Public/Icons/Extension.png',
        'security' => [
            'ignorePageTypeRestriction' => true,
        ],
    ],
    'types' => [
        '1' => [
            'showitem' => 'title,rollnumber,start_date, end_date, cgpa,employ, university, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language, sys_language_uid, l10n_parent, l10n_diffsource, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access, hidden, starttime, endtime'
        ],
    ],
    'columns' => [
        'sys_language_uid' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.language',
            'config' => [
                'type' => 'language',
            ],
        ],
        'l10n_parent' => [
            'displayCond' => 'FIELD:sys_language_uid:>:0',
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.l18n_parent',
            'config' => [
                'type' => 'select',
                'renderType' => 'selectSingle',
                'default' => 0,
                'items' => [
                    ['label' => '', 'value' => 0]
                ],
                'foreign_table' => 'tx_employee_domain_model_education',
                'foreign_table_where' => 'AND {#tx_employee_domain_model_education}.{#pid}=###CURRENT_PID### AND {#tx_employee_domain_model_education}.{#sys_language_uid} IN (-1,0)',
            ],
        ],
        'l10n_diffsource' => [
            'config' => [
                'type' => 'passthrough',
            ],
        ],
        'hidden' => [
            'config' => [
                'type' => 'check',
                'items' => [
                    ['label' => 'Disable'],
                ],
            ]
        ],
        'starttime' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.starttime',
            'config' => [
                'type' => 'input',
                'renderType' => 'datetime',
                'eval' => 'datetime,int',
                'default' => 0,
                'behaviour' => [
                    'allowLanguageSynchronization' => true
                ]
            ],
        ],
        'endtime' => [
            'exclude' => true,
            'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.endtime',
            'config' => [
                'type' => 'input',
                'renderType' => 'datetime',
                'eval' => 'datetime,int',
                'default' => 0,
                'range' => [
                    'upper' => mktime(0, 0, 0, 1, 1, 2038)
                ],
                'behaviour' => [
                    'allowLanguageSynchronization' => true
                ]
            ],
        ],
        'title' => [
            'l10n_mode' => 'prefixLangTitle',
            'label' => 'Title',
            'description' => 'Title',
            'config' => [
                'type' => 'input',
                'behaviour' => [
                    'allowLanguageSynchronization' => true,
                ],
            ]
        ],
        'rollnumber' => [
            'l10n_mode' => 'prefixLangTitle',
            'label' => 'Roll Number',
            'description' => 'Enter Seat Number',
            'config' => [
                'type' => 'input',
                'behaviour' => [
                    'allowLanguageSynchronization' => true,
                ],
            ]
        ],
        'start_date' => [
            'label' => 'Start Date',
            'config' => [
                'type' => 'datetime',
                'format' => 'date',
                'eval' => 'int',
                'default' => 0,
            ]
        ],
        'end_date' => [
            'label' => 'End Date',
            'config' => [
                'type' => 'datetime',
                'format' => 'date',
                'eval' => 'int',
                'default' => 0,
            ]
        ],
        "cgpa" => [
            'label' => 'CGPA',
            'config' => [
                'type' => 'number',
                'format' => 'decimal'
            ]
        ],
        "university" => [
            'label' => 'University',
            'config' => [
                'type' => 'select',
                'renderType' => 'selectSingle',
                'items' => [
                    [
                        'label' => 'University 1',
                        'value' => 'uni-1',
                    ],
                    [
                        'label' => 'University 2',
                        'value' => 'uni-2',
                    ],
                    [
                        'label' => 'University 3',
                        'value' => 'uni-3',
                    ],
                ],
            ],
        ], 
];

After saving these files, please toggle the extension (Deactivate once and then activate again) or go to Maintainance > Flush TYPO3 and PHP cache and click on the button “Flush cache”. As explained above after adding TCA files and toggling the extension, the table will have the default fields.

As mentioned above in point 6, after adding TCA files now the option should be visible to add records for the “Employee“. Let’s check if the option is available now or not in this example.

Go to Web > List > select the folder in which you want to store the records, and press on the “+Create new record” button.

After selecting “Employe“, the below screen will appear and we will be able to add the employee record.

Here you can see that there is no option to add the education for the employee. So we need to establish the relation between the tables.

Relation with table

As per our definition, one employee can have multiple educations, so we should give options in “Employe” form to add multiple educations, and to do that we should establish the relation 1:n between 2 tables.
We can establish the relationship by updating the 3 files which are
i ) employee/ext_tables.sql, 
ii )employee/Configuration/TCA/tx_employee_domain_model_education.php, and 
iii ) employee/Configuration/TCA/tx_employee_domain_model_employ.php. Let’s see the updates in files in details

 

1. employee/ext_tables.sql

  1. Add fields in both tables to relate them to each other.
  2. Add employ field in education table and add the education field in employ table.
  3. Below is the code of SQL file after these updates.
CREATE TABLE tx_employee_domain_model_employ (
first_name varchar(255) DEFAULT '' NOT NULL,
last_name varchar(255) DEFAULT '' NOT NULL,
gender int(11) DEFAULT 0 NOT NULL,
birth_date int(11) DEFAULT '0' NOT NULL,
joining_date int(11) DEFAULT '0' NOT NULL,
image int(11) unsigned DEFAULT '0',
bio text,
experiance text,
salary double(11,2) DEFAULT 0.00 NOT NULL,
languages int(11) DEFAULT 0 NOT NULL,
country tinytext,
educations int(11) DEFAULT 0 NOT NULL,
);


CREATE TABLE tx_employee_domain_model_education (
title varchar(255) DEFAULT '' NOT NULL,
rollnumber varchar(255) DEFAULT '' NOT NULL,
start_date int(11) DEFAULT '0' NOT NULL,
end_date int(11) DEFAULT '0' NOT NULL,
cgpa double(11,2) DEFAULT 0.00 NOT NULL,
university tinytext,
employ int(11) DEFAULT '0' NOT NULL,
);

2. employee/Configuration/TCA/tx_employee_domain_model_education.php

  1. Add field in TCA file of education to connect with employ table.
"employ" => [
'label' => 'Employ',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'tx_employee_domain_model_employ',
'foreign_table_where' => 'AND {#tx_employee_domain_model_employ}.{#pid}=###CURRENT_PID### AND {#tx_employee_domain_model_employ}.{#sys_language_uid}='###REC_FIELD_sys_language_uid###'',
],
]

3. employee/Configuration/TCA/tx_employee_domain_model_employ.php

  1. Add field in TCA file of employ to connect with employ table.
'educations' => [
'label' => 'Educations',
'description' => 'Add Educations',
'config' => [
'type' => 'inline',
'foreign_table' => 'tx_employee_domain_model_education',
'foreign_field' => 'employ',
'appearance' => [
'showSynchronizationLink' => true,
'showAllLocalizationLink' => true,
'showPossibleLocalizationRecords' => true,
],
],
],

After these changes in files, please toggle the extension (deactivate and activate again), or go to Maintainance > Flush TYPO3 and PHP cache and click on the button “Flush cache”, then the field of education will be added to the “employ” form.

Share

Leave a Comment