TYPO3 - Hooks in Extbase Extensions
Links[Bearbeiten]
Hooks in Extbase Extensions[Bearbeiten]
Eine Alternative für Extbase Extensions sind die Signal Slots. Aber vorsicht die in verschiedenen Tutorials vorgeschlagenen Slots für Backend deletion funktionieren nicht wenn man im Backend arbeitet (6.2.13) Weil die Speicherung nicht darüber abgewickelt wird.
Kurzübersicht[Bearbeiten]
Quelle http://www.schmutt.de/456/hook-mit-extbase-implementieren/ (2015-06)
1) unter Classes/Hooks eine Klasse anlegen: HooksHandler.php
2) Hook-Klasse und Funktion implementieren
class Tx_MyExtension_Hooks_HooksHandler {
/**
* Hooktest
* @param array $params
* @param object $Obj
* @return string
*/
public function hooktest($params, &$Obj)
{
return "Hallo aus Extbase";
}
}
3) Hook in ext_localconf.php registrieren
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['other_extension']['my_hook_name'][] =
'EXT:my_extension/Classes/Hooks/hooksHandler.php:Tx_MyExtension_Hooks_HooksHandler->hooktest';
Wer den Hook auch selber implementieren möchte, hier noch der Code dazu:
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['other_extension']['my_hook_name']) && is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['other_extension']['my_hook_name'])) {
$_params = array(
'param1' => $value1,
);
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['other_extension']['my_hook_name'] as $_funcRef) {
$content .= t3lib_div::callUserFunction($_funcRef, $_params, $this);
}
}
Ein Hook kann übrigens auch eine ganz gute Möglichkeit sein, um eine alte piBased Extension mit einer neuen ExtBase Extension zu erweitern.
In ExtBase gibt es eine Alternative zu Hooks: das Signal Slot Pattern. Infos zu diesem Pattern in den Links unten. Leider hab ich es noch nicht geschafft das aus einer piBased Extension aufzurufen, wer weiß wie das geht, nur her mit den Codes!
Links zu Hooks: http://www.hann3mann.de/artikel/einen-hook-unter-typo3-programmieren/
http://typo3blogger.de/alles-uber-hooks/
http://www.typo3lexikon.de/typo3-tutorials/tipps-und-tricks/hooks.html
Links zu Signal / Slot Pattern: http://typo3blogger.de/signal-slot-pattern/
http://blog.foertel.com/2011/10/using-signalslots-in-extbase/
http://blog.tolleiv.de/2011/11/signal-slots-in-extbase/
Beispiel: Systemhook beim abarbeiten on Datensätzen (z.b. speichern)[Bearbeiten]
Hinweis: In Extbase wird im Namespace der Ordner Classes im Pfad weggelassen weil extbase davon ausgeht, dass dieser existiert.
localconf.php
/*$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] =
'EXT:gbgeocode/Classes/Hooks/processDatamap.php:processDatamap'; //old school works */
//processDatamap_postProcessFieldArray
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] =
'Geobit\\Gbgeocode\\Hooks\\ProcessDatamap';//Namespace version works
typo3conf/ext/gbgeocode/Classes/Hooks/ProcessDatamap.php
<?php
namespace Geobit\Gbgeocode\Hooks;
class ProcessDatamap {
/**
* 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 processDatamap_postProcessFieldArray($command, $table, $id, $value, $dataHandler) {
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($command,"command");
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($table,"table");
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($id,"id");
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($value,"value");
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($dataHandler,"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
}
}
}
?>
Beispiel - Hook beim Löschen eines Datensatzes im Backend[Bearbeiten]
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
Beispiele localconf.php[Bearbeiten]
Verschiedene Methoden zum Registrieren:
/* Hook to enable automatic geolocation function at storing */
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:gbmaps/hooks/class.tx_gbmaps_tcemainhooks.php:tx_gbmaps_tcemainhooks'; // Old School pre Extbase
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] =\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY).'Classes/Hooks/class.tx_gbmaps_hooks.php:tx_gbmaps_hook';
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = 'Geobit\\gbmaps\\Hooks\\ProcessCmdmap';
//works this way:
// $GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = path_to_ext_or_vendor\\extbane\\Hooks\\HookClass:function
Beispiele für den Hook (not verified)[Bearbeiten]
<?php
namespace Geobit\Gbmaps\Hooks;
class ProcessCmdmap {
function processCmdmap(){
t3lib\_div::devLog("Message","gbmaps",2,"test");
}
function processCmdmap_postProcess($command, $table, $id, $value, $dataHandler) {
if(!empty($command))die();
t3lib\_div::devLog("Message","gbmaps",2,$command);
t3lib_utility_Debug::debugInPopUpWindow($command, "command");
//\TYPO3\CMS\Core\Utility\DebugUtility::debug($command);
Tx_Extbase_Utility_Debugger::var_dump($myModel);
//Tx_Extbase_Utility_Debugger::var_dump($command);
//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($command);
//Write debug to a file
$textFileCnt = 'DEBUG ';
$textFileCnt .= 'Labe2: '.'manipulated text content, that needs to write to file';
$file1 = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('fileadmin/debug/debug.txt');
\TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($file1, $textFileCnt);
// Example to do s.th. when delete
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
}
// Example to use Message queue
$something_wrong = 0 ;
if($something_wrong){
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'OOOPS','Sth. is wrong',\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,TRUE);
}else{
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'OK','Everything goes alright',\TYPO3\CMS\Core\Messaging\FlashMessage::OK,// ::ok is default => optional
TRUE);
}
\TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
}
}
?>