TYPO3 - Hooks in Extbase Extensions

Aus Wikizone
Wechseln zu: Navigation, Suche

Hooks in Extbase Extensions

Hinweis: eine Alternative für Extbase Extensions sind die Signal Slots.

Kurzübersicht

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 - Hook beim Löschen eines Datensatzes im Backend

http://stackoverflow.com/questions/25591992/typo3-extbase-individual-code-on-backend-deletion-of-an-object

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