Oliver
Oliver

Reputation: 865

TYPO3 how to add virtual column to the TCA?

how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element. I need something like this:

$fields = [
    'counts7d' => [
        'exclude' => false,
        'label' => 'last 7 days',
        'config' => [
            'type' => 'none',
            'procFunc' => '\Namespace\MyClass->MyMethod',
            'readOnly' => true,
            'params' => [
                'period => '7d'
            ]
        ]
    ],
    'counts30d' => [
        'exclude' => false,
        'label' => 'last 30 days',
        'config' => [
            'type' => 'none',
            'procFunc' => '\Namespace\MyClass->MyMethod',
            'readOnly' => true,
            'params' => [
                'period => '30d'
            ]
        ]
    ],
];



pseudo function:
public function myMethod($element, $params){

$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";

return sql_count…

}

The field should only be informative for the backend users.

Does anyone have an idea?

Thanks Oliver

Upvotes: 0

Views: 578

Answers (2)

patryno
patryno

Reputation: 154

The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.

Upvotes: 0

Mathias Brodala
Mathias Brodala

Reputation: 6480

The TCA field type user is exactly what you are looking for:

'counts7d' => [
    'exclude' => false,
    'label' => 'last 7 days',
    'config' => [
        'type' => 'user',
        'userFunc' => \Namespace\MyClass::class . '->MyMethod',
        'parameters' => [
            'period => '7d',
        ],
    ],
],

Upvotes: 1

Related Questions