首页 文章

TYPO3 Extbase单个代码在后端删除对象

提问于
浏览
3

当我从TYPO3后端的列表视图中删除一个Extbase域对象时,我想执行一些单独的代码 .

认为它可以/将通过覆盖相应的存储库中的 remove( $o ) 方法来工作

public function remove( $object ) {
    parent::__remove( $object );
    do_something_i_want();
}

,但我认为这不会奏效 . 看起来存储库方法只被我的扩展操作调用/使用(例如,如果我在FE或BE插件中有一些删除操作),而不是当对象刚从后端的列表视图中删除时?我不使用(截至目前)任何FE / BE-plugin / -actions - 只在我的存储文件夹的后端列表视图中使用简单的添加/编辑/删除功能 .

背景:我有例如两个模型有一些1:n关系(比方说 singersong ),其中一个对象包含一些上传文件( album_cover >指向 /uploads/myext/ 文件夹中的文件URL);使用 @cascade 可以正常删除属于被删除的 singersong ,但它不会触及上传的文件(仅限) song.album_cover - 随着时间的推移会导致相当多的浪费 . 所以我很想做某种事情.1066216_ . (同样的事情将适用于删除let,例如单个 song 并且它是 album_cover -file . )

听起来很简单和常见,但我只是不落后于它并且没有发现任何有用的东西 - 会喜欢一些提示/指向正确的方向:-) .

2 回答

  • 5

    列表视图在其操作期间使用TCEmain钩子,因此您可以使用它们中的一个来交叉删除操作,即:processCmdmap_deleteAction

    • typo3conf/ext/your_ext/ext_tables.php 注册你的钩子类
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = 'VENDORNAME\\YourExt\\Hooks\\ProcessCmdmap';
    
    • 创建一个具有有效命名空间和路径的类(根据上一步)
      档案: 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
            }
        }
    }
    
    • 注册新的hook类后不要忘记清除系统缓存
  • 3

    除了biesiors答案,我想指出,还有一个signalSlot . 所以你宁愿注册那个信号而不是挂钩到tcemain .

    在你的 ext_localconf.php 中:

    $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'
    );
    

    所以在你的Slot中你有这个PHP文件:

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

    注意, $object 将是刚刚从数据库中删除的$对象 .

    有关更多信息,请参阅https://usetypo3.com/signals-and-hooks-in-typo3.html

相关问题