Search code examples
phptypo3hooktypo3-7.6.xtypo3-extensions

TYPO3 TCA execute hook after object save in backend


I want to manipulate some values and execute a custom function if an object is saved trought the backend. I found through my google search that I have to specify this in my ext_localconfphp :

$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass']['extkey'] = 'Vendor\\Extension\\Hook\\TCEmainHook';
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass']['extkey'] = 'Vendor\\Extension\\Hook\\TCEmainHook';

Additionally, I created the following class in my extension /Classes/Hook/TCEmainHook.php

<?php
namespace Vendor\Extension\Hook;

class TCEmainHook {
public function processCmdmap_postProcess(
    $command, $table, $id, $value, 
    TYPO3\CMS\Core\DataHandling\DataHandler &$pObj) {
       echo '<pre>';
           var_dump($command);
       echo '<pre>';
       die();
    }
}

But no matter which of the following options I try, I just get an empty backend frame after I save an object:

$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass']['NXS\\NxsReferenzen\\Hook\\TCEmainHook'] = 'EXT:nxs_referenzen/Classes/Hook/TCEmainHook.php';
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][''] = 'NXS\\NxsReferenzen\\Hook\\TCEmainHook';
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][''] = 'EXT:nxs_referenzen/Classes/Hook/TCEmainHook.php:\NXS\\NxsReferenzen\\Hook\\TCEmainHook';

I don't get what I am doing wrong. Does someone have any suggestions?

solution for reference: Thanks to jokumer's suggestion I looked up which and how other hooks are being loaded in the BE modul 'Configuration'. I saw that my hook looked different from the others so I checked how the powermail hook has been defined (that's another extension I'm using) and with the following changes the hook is finally working:

$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:nxs_referenzen/Classes/Hook/TCEmainHook.php:NXS\\NxsReferenzen\\Hook\\TCEmainHook';
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = 'EXT:nxs_referenzen/Classes/Hook/TCEmainHook.php:NXS\\NxsReferenzen\\Hook\\TCEmainHook';

Solution

  • Register your hook class in local configuration (ext_localconf.php):

    $GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][''] = 'NXS\\NxsReferenzen\\Hook\\TCEmainHook';
    $GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][''] = 'NXS\\NxsReferenzen\\Hook\\TCEmainHook';
    

    Ensure your hook class has namespace declaration:

    <?php
    namespace NXS\NxsReferenzen\Hook;
    
    class TCEmainHook {
        public function processCmdmap_postProcess($command, $table, $id, $value, \TYPO3\CMS\Core\DataHandling\DataHandler &$pObj) {
            echo '<pre>';
            var_dump($command);
            echo '<pre>';
            die();
        }
    }