TYPO3 Extbase custom code for backend object deletion
I would like to execute some separate code where one of my Extbase domain objects is removed from the list view in the TYPO3 backend.
Thought it could / would work by overwriting the method remove( $o )
in the appropriate repository like
public function remove( $object ) {
parent::__remove( $object );
do_something_i_want();
}
but that won't work. It looks like the repository methods are only called / used by the actions of my extension (for example if I had any delete action in the FE or BE plugin), but not when the object is simply removed from the list view in the backend? I don't (until now) use any FE / BE-plugin / -actions - just simple add / edit / delete functions in the backed list view of my storage folder.
Background: I have, for example, two models with some 1: n ratio (say singer
and song
), where one object includes some uploaded file ( album_cover
> pointing to the url of a file in a folder /uploads/myext/
); the use is @cascade
great for deleting every song
one owned singer
that gets deleted, but it won't touch the downloaded file (only) for song.album_cover
- which will lead to quite some loss over time. So I would like to do something like onDeletionOfSinger() { deleteAllFilesForHisSongs(); }
-thing. (The same applies when removing let say one song
and its album_cover
-file.)
Sounds pretty easy and frequent, but I just keep up with it and haven't found anything useful - would like to hint / point a little in the right direction :-).
source to share
The list view uses TCEmain interceptors during its operations, so you can use one of them to intersect the delete action, that is: processCmdmap_deleteAction
-
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';
-
Create a class with a valid namespace and path (according to the 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 } } }
-
Remember to clear the system cache after registering a new hook class
source to share
In addition to biesiors' answers, I want to point out that there is a signalSlot for this as well. Therefore, you can register on this signal rather than connecting to tcemain.
in 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 that thet $object
will be a $ object that was simply removed from the database.
For more information see https://usetypo3.com/signals-and-hooks-in-typo3.html
source to share