Examples: TCA column type category

Simple category field

In the following example a category tree is displayed and multiple categories can be selected.

EXT:my_extension/Configuration/TCA/Overrides/someTable.php
<?php

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

$GLOBALS['TCA'][$myTable]['columns']['categories'] = [
    'config' => [
        'type' => 'category',
    ],
];

ExtensionManagementUtility::addToAllTCAtypes(
    $myTable,
    'categories'
);
Copied!

The relationship gets stored in the intermediate table sys_category_record_mm. Category counts are only stored on the local side.

One to one relation category field

In the following example a category tree is displayed, but only one category can be selected.

EXT:my_extension/Configuration/TCA/Overrides/someTable.php
<?php

$GLOBALS['TCA'][$myTable]['columns']['mainCategory'] = [
    'config' => [
        'type' => 'category',
        'relationship' => 'oneToOne',
    ],
];
Copied!

Category field used in FlexForm

It is possible to use the type category in FlexForm data structures. Due to some limitations in FlexForm, the manyToMany relationship is not supported. Therefore, the default relationship - used if none is defined - is oneToMany.

An example of the "oneToMany" use case is EXT:news, which allows to only display news of specific categories in the list view:

EXT:my_extension/Configuration/FlexForm/SomeFlexForm.xml
<T3DataStructure>
    <ROOT>
        <sheetTitle>aTitle</sheetTitle>
        <type>array</type>
        <el>
            <categories>
                <config>
                    <type>category</type>
                </config>
            </categories>
        </el>
    </ROOT>
</T3DataStructure>
Copied!