TYPO3 Hook im Backend beim Speichern von Datensätzen
Hooks in Extbase Extensions
Hinweis: eine Alternative für Extbase Extensions sind die Signal Slots.
Beispiel: Hook beim Löschen eines Datensatzes im Backend
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';
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
}
}
}
Don't forget to clear system cache after registering new hook's class
Verfügbare Hooks: http://typo3.org/api/typo3cms/namespace_t_y_p_o3_1_1_c_m_s_1_1_version_1_1_hook.html
Hook im Backend (old school Extensions)
Quelle: http://www.koller-webprogramming.ch/tipps-tricks/typo3-extension-entwicklung/hook-im-backend/ (Zugriff: 6/2011)
Dieses Beispiel zeigt wie man eine Reaktion erreicht nachdem man einen Datensatz im Backend abspeichert. Dazu gibt es einen Hook der beim speichern eines Datensatzes im Backend ausgeführt wird.
Hook in der ext_localconf.php Registrieren
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:ext_name/hooks/class.tx_ext_name_tcemainhooks.php:tx_ext_name_tcemainhooks';
Wird der entsprechende Datensatz nun im Backend gespeichert, wird im Hintergrund der Hook ausgeführt, in unserem Fall also die Datei class.tx_ext_name_tcemainhooks.php.
Allerdings wird hier zu Anschauungszwecken nichts anderes gemacht als der Array ausgegeben der die relevanten Daten beherbergt, also die Daten des gespeicherten Datensatzes.
Nun kann in der Datei hooks/class.tx_ext_name_tcemainhooks.php z.B. ein Feld des gespeicherten Datensatzes z.B. nach einem Wert abgefragt werden, wo dann je nachdem die entsprechende Funktion aufgerufen wird.
So ist es möglich eines der Felder zu überschreiben, Mail auszulösen, oder sonst eine Funktion auszuführen und zwar unmittelbar nachdem der Datensatz im Backend gespeichert wurde.
Die Datei hooks/class.tx_ext_name_tcemainhooks.php
class tx_ext_name_tcemainhooks {
function processDatamap_preProcessFieldArray(&$FieldArray, $table, $id, &$tceobj) {
print_r($FieldArray);
}
}