TYPO3 Extbase individual code on backend-deletion of an object

让人想犯罪 __ 提交于 2019-12-01 09:07:20

List view uses TCEmain hooks during its operations, so you can use one of them to intersect delete action, i.e.: processCmdmap_deleteAction

  1. Register your hooks class in typo3conf/ext/your_ext/ext_tables.php

    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = 'VENDORNAME\\YourExt\\Hooks\\ProcessCmdmap';
    
  2. Create a class with valid namespace and path (according to previous step)
    file: typo3conf/ext/your_ext/Classes/Hooks/ProcessCmdmap.php

    <?php
    namespace VENDORNAME\YourExt\Hooks;
    
    class ProcessCmdmap {
       /**
        * hook that is called when an element shall get deleted
        *
        * @param string $table the table of the record
        * @param integer $id the ID of the record
        * @param array $record The accordant database record
        * @param boolean $recordWasDeleted can be set so that other hooks or
        * @param DataHandler $tcemainObj reference to the main tcemain object
        * @return   void
        */
        function processCmdmap_postProcess($command, $table, $id, $value, $dataHandler) {
            if ($command == 'delete' && $table == 'tx_yourext_domain_model_something') {
                // Perform something before real delete
                // You don't need to delete the record here it will be deleted by CMD after the hook
            }
        }
    } 
    
  3. Don't forget to clear system cache after registering new hook's class

In addition to biesiors answer I want to point out, that there is also a signalSlot for this. So you can rather register on that signal than hooking into tcemain.

in your ext_localconf.php put:

$signalSlotDispatcher =
            \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
$signalSlotDispatcher->connect(
    'TYPO3\CMS\Extbase\Persistence\Generic\Backend',
    'afterRemoveObject',
    'Vendor\MxExtension\Slots\MyAfterRemoveObjectSlot',
    'myAfterRemoveObjectMethod'
);

So in your Slot you have this PHP file:

namespace Vendor\MxExtension\Slots;
class MyAfterRemoveObjectSlot {
    public function myAfterRemoveObjectMethod($object) {
         // do something
    }
}

Note thet $object will be the $object that was just removed from the DB.

For more information, see https://usetypo3.com/signals-and-hooks-in-typo3.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!