Login / Status
developer.Resource
Home . Development . Articles . How to use existing hooks
Sponsors
hosted by punkt.deTYPO3 and Open Source MagazineAOE Media

A real world example

In case you still don't know what hooks are about, maybe a real world example will give you a clue. Let's assume you want to create an extension which provides a basic workflow.  

Authors and chief editors

Your website's authors are allowed to create pages, but those will be hidden by default. The hidden field in the pages table is excluded by using the exclude-field mechanism and thus not available to these authors.

The chief-editor has more rights, as he is in charge of checking the content, his authors have produced. Whenever he accepts an article, he just un-hides the page and thus makes it visible on your website. 

This mini-workflow works perfectly until an author, who still has the right to edit his page, modifies it. Instantly the new content will be visible to the public bypassing the control of the chief-editor! 

Our goal is now to make sure, the page will be hidden automatically, whenever an author, not being member of the chief-editors group, modifies the page or one of it's content elements. 

Locating the hook

To achieve that goal, we first have to find out what happens when you update a page or a content element in the backend.  

Because you know the core quite well and have read Inside TYPO3 and TYPO3 Core APIs, you know that submitted data from the backend forms is being processed in the class t3lib_tcemain. I won't discuss the details of TCEmain in this article, but I guess you'll understand our hook we're going to use without deeper knowledge of TYPO3's core classes.

The place to begin our search for a possible hook therefore is a method called process_datamap which resides in t3lib/class.t3lib_tcemain.php.

I've been here

Surprisingly someone else already had a need for some hook in this method: browsing through the lines, we find a neat pre-processing hook implemented by some core developer: 

    // Hook: processDatamap_postProcessFieldArray

reset($hookObjectsArr);

while (list(,$hookObj) = each($hookObjectsArr)) {

    if (method_exists ($hookObj, 'processDatamap_postProcessFieldArray')) {

        $hookObj->processDatamap_postProcessFieldArray ($status, $table, $id, $fieldArray, $this);

    } 

This was great luck of course, because usually you don't find a pre-made hook which perfectly suits your needs. But if there is a need for a new entry point and that hook makes sense to implement, just contact the author of that paticular part of code and he'll gladly provide you with a hook.