How to utilize a hook defined in a TYPO3 Extension

This article gives a short overview on how to utilize a hook that is generally written in TYPO3 extension. We will use an available to_news hook for this.

Generally TYPO3 and it’s extensions are written to be extended. Hooks are one such mechanism which helps developers to extend features of an extension without modifying/patching the extension itself. Most well known extensions have hooks that are defined to help us easily extend it’s features. In some cases hooks my not be available at all, but we may have an XCLASS that would be of great help if a hook is not present.

NOTE1:
You cannot use a hook if it is not already defined! So we can use XCLASS in such case.

NOTE2:
The steps defined here uses a tt_news hook as an example, however this method would generally work on all extensions that have a hook.

STEP 1: Check which hook to use.

I added some new fields, and I wanted them to be displayed in list/latest and single view templates of news.
I selected the “extraItemMarkerProcessor” hook that seemed to fit this need.

STEP 2: Check the inputs we get and add appropriate function inside out class

The hook in tt_news was defined as follows:

foreach($GLOBALS[‘TYPO3_CONF_VARS’][‘EXTCONF’][‘tt_news’][‘extraItemMarkerHook’] as $_classRef) {

$_procObj = & t3lib_div::getUserObj($_classRef);
$markerArray = $_procObj->extraItemMarkerProcessor($markerArray, $row, $lConf, $this);

}

So all I did was to write a class with this method/function as follows:

class tx_ttnewshooks {

function extraItemMarkerProcessor($markerArray, $row, $lConf, $ttnews){
// MY CODE GOES HERE

return $markerArray;

}

}

STEP 3: Include our hook

It is now time to include our hook, so that tt_news knows that we would like to use this hook.
Here is what I did:

A. Use kickstarter to create your extension’s skeleton, let me call this as “ttnewshooks“.
ex: By adding information etc…
NOTE: Unfortunetely, I will not be covering how to create an extension with kickstarter.

B. Create a new file called ext_localconf.php (if it is not already here)

C
. Add the following code inside this file:

<?php

if (!defined (“TYPO3_MODE”)) die (“Access denied.”);

if (TYPO3_MODE!=’BE’) {
require_once(t3lib_extMgm::extPath(‘ttnewshooks’).’class.ttnewshooks.php’);
}

$TYPO3_CONF_VARS[‘EXTCONF’][‘tt_news’][‘extraItemMarkerHook’][] = ‘tx_ttnewshooks’;

?>

STEP 4: Time to check if things work.
Generally there should not be any issues.
NOTE: Make sure that our new extension is installed, else this will never work!

Further Reference:
http://typo3.org/development/articles/how-to-use-existing-hooks/