How to find Hooks in TYPO3

Hooks inside of TYPO3 CMS allow you to hook into existing processes of the core, or of extensions, to manipulate the processes.

This post will explain in more depth what hooks are and how you can find and use them.

What are Hooks in TYPO3?

TYPO3 has a lot of processes like evaluating data, authenticating users, displaying content and so on. All of this processes are handled inside of TYPO3. In some use cases you want to hook into the process and manipulate the process. E.g. you want to modify data inserted into the backend, before they get persisted into the database.

This can be achieved by using a hook to modify the process. The hook allows you to include your custom PHP into the process, which will get executed.

The execution of Hooks is typically implemented like the following:

1
2
3
4
5
6
7
 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['devLog'])) {
     $params = array('msg' => $msg, 'extKey' => $extKey, 'severity' => $severity, 'dataVar' => $dataVar);
     $fakeThis = false;
     foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['devLog'] as $hookMethod) {
         self::callUserFunction($hookMethod, $params, $fakeThis);
     }
 }

So your configured method will be called with some arguments, on line 5 in above example. Most of the time the arguments will be references enabling you to modify them.

Hooks differ in two ways:

  1. Some hooks need your class to implement a specific interface, or method. Others, like above will just call the function or method you have provided, like a userfunc.
  2. Also some hooks will provide arguments via reference and others via copy.

A simple example

Let’s assume the following example: You want to provide latitude and longitude to frontend users, auto generated based on their address. That can be achieved by using a hook inside of \TYPO3\CMS\Core\DataHandling\DataHandler.

Configure the hook in ext_localconf.php of your extension like:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][$_EXTKEY]
    = 'DanielSiepmann\FeuserLocations\Hook\DataMapHook';

This will register the class DanielSiepmann\FeuserLocations\Hook\DataMapHook to be processed. Inside of the class we have the following method, called during the process:

/**
 * Hook to add latitude and longitude to locations.
 *
 * @param string $action The action to perform, e.g. 'update'.
 * @param string $table The table affected by action, e.g. 'fe_users'.
 * @param int $uid The uid of the record affected by action.
 * @param array $modifiedFields The modified fields of the record.
 *
 * @return void
 */
public function processDatamap_postProcessFieldArray( // @codingStandardsIgnoreLine
    $action,
    $table,
    $uid,
    array &$modifiedFields
) {
    if(! $this->processGeocoding($table, $action, $modifiedFields)) {
        return;
    }

    $geoInformation = $this->getGeoinformation(
        $this->getAddress($modifiedFields, $uid)
    );

    $modifiedFields['lat'] = $geoInformation['geometry']['location']['lat'];
    $modifiedFields['lng'] = $geoInformation['geometry']['location']['lng'];
}

This method will get called for all data changed through DataHandler before they are processed by the DataHandler.

Caution

As this method get called for all data, you should check whether to execute the method. Same is true for some other hooks like database queries. Your system will slow down without these checks called guards.

How to find hooks

Hooks are always configured through $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'], so finding hooks is as easy as a search for SC_OPTIONS. Of course you need to understand the surrounding code and where your hook should be executed to find the right place.

E.g. execute the following in your shell:

grep -n -C 5 "SC_OPTIONS" -r vendor/typo3/cms

Beside the core, also extensions might provide hooks, so adjust the path, vendor/typo3/cms, to search inside an extension.

Also all registered hooks can be found inside the backend “Configuration” module. Just select the TYPO3_CONF_VARS in dropdown and search for SC_OPTIONS. By registed I mean hooks that are already in use, it’s not a full list of available hooks.

Signal Slots

Beside the concept of hooks, TYPO3 also provides the concept of Signal Slots. I will not document that concept here, there are already some blog posts about the topic:

In general it’s the same idea, just implemented in an object oriented way.

Further reading

Checkout the official documentation at Hooks.

Also check out examples for userfunctions.

Also you can check how other developers make usage of hooks, e.g. in the example extension wv_feuser_locations.