Customization Examples
There are many customization examples in the documentation, but this section provides the most complete examples.
Example 1: Extending the fe_users table
The "examples" extension adds two fields to the "fe_users" table.
Here is the complete code, taken from file
Configuration/TCA/Overrides/fe_users.php:
<?php
defined('TYPO3') or die();
// Add some fields to fe_users table to show TCA fields definitions
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('fe_users',
[
'tx_examples_options' => [
'exclude' => 0,
'label' => 'LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_options',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'items' => [
['',0,],
['LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_options.I.0',1,],
['LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_options.I.1',2,],
['LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_options.I.2','--div--',],
['LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_options.I.3',3,],
],
'size' => 1,
'maxitems' => 1,
],
],
'tx_examples_special' => [
'exclude' => 0,
'label' => 'LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:fe_users.tx_examples_special',
'config' => [
'type' => 'user',
// renderType needs to be registered in ext_localconf.php
'renderType' => 'specialField',
'parameters' => [
'size' => '30',
'color' => '#F49700',
],
],
],
]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'tx_examples_options, tx_examples_special'
);
Read why the check for the TYPO3 constant is necessary.
Note
The second example,
tx_, only works when
render has been registered, implemented, and registered in
ext_localconf.php. Please refer to Custom inputs (user).
The first method call adds fields using
Extension. This registers the fields
in
$GLOBALS. The parameters are:
- Name of the table to which the fields should be added.
- An array of the fields to be added. Each field is in TCA syntax for columns.
At this point the fields have been registered but not used anywhere, so they need to be added
to the "types" definition of the
fe_ table by
calling
Extension. The parameters are:
- Name of the table to which the fields should be added.
- Comma-separated string of fields, the same syntax used in the showitem property of types in TCA.
- Optional: record types of the table where the fields should be added, see types in TCA for details.
- Optional: position (
'before'or'after') in relation to an existing field (after:) or palette (myfield after:).palette: mypalette
Example code:
<?php
declare(strict_types=1);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'tx_myextension_options, tx_myextension_special',
'',
'after:password',
);
If the fourth parameter is omitted or the field is not found, new fields are added to the bottom of the form. If the table uses tabs, new fields are added to the bottom of the Extended tab. This tab is created automatically if it does not exist.
These method calls do not create fields in the database. To do
this, the new fields must be defined in the ext_tables.sql file of the extension:
CREATE TABLE fe_users (
tx_examples_options int(11) DEFAULT '0' NOT NULL,
tx_examples_special varchar(255) DEFAULT '' NOT NULL
);
Note
The above example uses the SQL
CREATE TABLE statement. This is the
way TYPO3 expects it to be. The Extension Manager will automatically
transform this into an
ALTER TABLE statement when it detects that the
table already exists.
The following screenshot shows the position of the two new fields when editing a "fe_users" record:
The new fields added at the bottom of the "Extended" tab
The next example shows how to position a field more precisely.
Example 2: Extending the tt_content Table
In this second example, we will add a "No print" field to all content
element types. First of all, we add its SQL definition in
ext_tables.sql:
CREATE TABLE tt_content (
tx_examples_noprint tinyint(4) DEFAULT '0' NOT NULL
);
Then we add it to the
$GLOBALS in Configuration/TCA/Overrides/tt_content.php:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
[
'tx_examples_noprint' => [
'exclude' => 0,
'label' => 'LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:tt_content.tx_examples_noprint',
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'items' => [
[
0 => '',
1 => ''
]
],
],
],
]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette(
'tt_content',
'access',
'tx_examples_noprint',
'before:editlock'
);
The code is similar to the first example, but the last method call
is different. The tables pages and
tt_ use palettes extensively. Therefore,
we call Extension
instead of Extension.
We need to specify the palette key as the second argument (access).
The new field is positioned by the fourth parameter
(before:). This will position the "no print" field before the
Restrict editing by non-Admins field, instead of putting it in the
Extended tab.
The result is the following:
The new field added next to an existing one
Note
Obviously this new field doesn't do anything yet. For it to do its job of
excluding a content element from being printed, it must modify the TypoScript
used to render the
tt_ table. Although this is outside the scope of this
manual, here is an example of what you could do, for the sake of
showing the complete process.
Assuming you are using "fluid_styled_content" (which is installed by default), you could add the following TypoScript to your template:
tt_content.stdWrap.outerWrap = <div class="noprint">|</div>
tt_content.stdWrap.outerWrap.if.isTrue.field = tx_examples_noprint
This will wrap a "div" tag with a "noprint" class around any content element that has its "No print" checkbox checked. The final step would be to declare the appropriate selector in the print-media CSS file so that "noprint" elements don't get displayed.
This is just an example of how the "No print" checkbox
can be implemented. It is meant to show that just adding
the field to the
$GLOBALS is not enough.