TYPOlight Open Source CMS > Understand > Developer's guide > Customizing TYPOlight > How to add custom functionality to TYPOlight using hooks

How to add custom functionality to TYPOlight using hooks

Hooks work similar to the callback functions of the Data Container Array. You can register one or more functions for a certain event and when the event is triggered, the callback functions are executed. Hooks allow you to add custom functionality to the core.

activateAccount

The activateAccount hook is triggered when a new front end account is activated. It passes the user object as argument and does not expect a return value. It is available from TYPOlight version 2.4.3.

// config.php
$GLOBALS['TL_HOOKS']['activateAccount'][] = array('MyClass', 'activateAccount');

// MyClass.php
public function activateAccount(Database_Result $objUser)
{
    // Do something
}

activateRecipient

The activateRecipient hook is triggered when a new newsletter recipient is added. It passes the e-mail address, the recipient IDs and the channel IDs as argument and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['activateRecipient'][] = array('MyClass', 'activateRecipient');

// MyClass.php
public function activateRecipient($strEmail, $arrRecipients, $arrChannels)
{
    // Do something
}

addCustomRegexp

The addCustomRegexp hook is triggered when an unknown regular expression is found. It passes the name of the regexp, the current value and the widget object as arguments and expects a boolean return value. It is available from TYPOlight version 2.6.2.

// config.php
$GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('MyClass', 'addCustomRegexp');

// MyClass.php
public function addCustomRegexp($strRegexp, $varValue, Widget $objWidget)
{
    if ($strRegexp == 'postal')
    {
        if (!preg_match('/^0-9{4,6}$/', $varValue))
        {
            $objWidget->addError('Field ' . $objWidget->label . ' should be a postal code.');
        }

        return true;
    }

    return false;
}

addLogEntry

The addLogEntry hook is triggered when a new log entry is added. It passes the message, the function and the action as arguments and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['addLogEntry'][] = array('MyClass', 'addLogEntry');

// MyClass.php
public function addLogEntry($strText, $strFunction, $strAction)
{
    // Do something
}

checkCredentials

The checkCredentials hook is triggered when a login attempt fails due to a wrong password. It passes the username and password as well as the user object as arguments and expects a boolean return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['checkCredentials'][] = array('MyClass', 'checkCredentials');

// MyClass.php
public function checkCredentials($strUsername, $strPassword, User $objUser)
{
    // Check against a global database
    if ($this->checkGlobalDbFor($strUsername, $strPassword))
    {
        return true;
    }

    return false;
}

closeAccount

The closeAccount hook is triggered when a user closes his account. It passes the user ID, the operation mode and the module as arguments and does not expect a return value. It is available from TYPOlight version 2.8.0.

// config.php
$GLOBALS['TL_HOOKS']['closeAccount'][] = array('MyClass', 'closeAccount');

// MyClass.php
public function closeAccount($intId, $strMode, $objModule)
{
    if ($strMode == 'close_delete')
    {
        // Do something
    }
}

createNewUser

The createNewUser hook is triggered when a new front end user registers on the website. It passes the ID of the new user and the data array as arguments and does not expect a return value. It is available from TYPOlight version 2.2.0.

// config.php
$GLOBALS['TL_HOOKS']['createNewUser'][] = array('MyClass', 'createNewUser');

// MyClass.php
public function createNewUser($intId, $arrData)
{
    // Modify the record
}

executePreActions

The executePreActions hook is triggered on Ajax requests that do not expect a response. It passes the name of the action as argument and does not expect a return value. It is available from TYPOlight version 2.6.1.

// config.php
$GLOBALS['TL_HOOKS']['executePreActions'][] = array('MyClass', 'executePreActions');

// MyClass.php
public function executePreActions($strAction)
{
    if ($strAction == 'update')
    {
        // Do something
    }
}

executePostActions

The executePostActions hook is triggered on Ajax requests that expect a response. It passes the name of the action and the data container object as arguments and does not expect a return value. It is available from TYPOlight version 2.6.1.

// config.php
$GLOBALS['TL_HOOKS']['executePostActions'][] = array('MyClass', 'executePostActions');

// MyClass.php
public function executePostActions($strAction, DataContainer $dc)
{
    if ($strAction == 'update')
    {
        // Do something
    }
}

generateFrontendUrl

The generateFrontendUrl hook is triggered when a front end URL is recreated. It passes the page object, the parameter string and the default URL as arguments and expects a string as return value. It is available from TYPOlight version 2.5.8.

// config.php
$GLOBALS['TL_HOOKS']['generateFrontendUrl'][] = array('MyClass', 'generateFrontendUrl');

// MyClass.php
public function generateFrontendUrl(Database_Result $objPage, $strParams, $strUrl)
{
    return str_replace('.html', '.xml', $strUrl);
}

generatePage

The generatePage hook is triggered before the main layout (fe_page) is compiled. It passes the page object, the layout object and a self-reference as arguments and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['generatePage'][] = array('MyClass', 'generatePage');

// MyClass.php
public function generatePage(Database_Result $objPage, Database_Result $objLayout, PageRegular $objPageRegular)
{
    // Do something
}

getAllEvents

The getAllEvents hook allows you to modify the result sets of calendar and event modules. It passes the current result set, the IDs of the parent items and the start and end time as arguments and expects a result set (array) as return value. It is available from TYPOlight version 2.6.4.

// config.php
$GLOBALS['TL_HOOKS']['getAllEvents'][] = array('MyClass', 'getAllEvents');

// MyClass.php
public function getAllEvents($arrEvents, $arrCalendars, $intStart, $intEnd)
{
    ksort($arrEvents);
    return $arrEvents;
}

getPageIdFromUrl

The getPageIdFromUrl hook is triggered when the URL fragments are evaluated. It passes the array of URL fragments as argument and expects an array of URL fragments as return value. It is available from TYPOlight version 2.5.4.

// config.php
$GLOBALS['TL_HOOKS']['getPageIdFromUrl'][] = array('MyClass', 'getPageIdFromUrl');

// MyClass.php
public function getPageIdFromUrl($arrFragments)
{
    return array_unique($arrFragments);
}

getSearchablePages

The getSearchablePages hook is triggered when the the search index is rebuilt. It passes the array of pages and the ID of the root page as arguments and expects an array of absolute URLs (!) as return value. It is available from TYPOlight version 2.2.0.

// config.php
$GLOBALS['TL_HOOKS']['getSearchablePages'][] = array('MyClass', 'getSearchablePages');

// MyClass.php
public function getSearchablePages($arrPages, $intRoot)
{
    return array_merge($arrPages, array('Additional pages'));
}

importUser

The importUser hook is triggered when a username cannot be found in the database. It passes the username, the password and the table name as arguments and expects a boolean return value. It is available from TYPOlight version 2.7.RC1.

// config.php
$GLOBALS['TL_HOOKS']['importUser'][] = array('MyClass', 'importUser');

// MyClass.php
public function importUser($strUsername, $strPassword, $strTable)
{
    if ($strTable == 'tl_member')
    {
        // Import user from an LDAP server
        if ($this->importUserFromLdap($strUsername, $strPassword))
        {
            return true;
        }
    }

    return false;
}

listComments

The listComments hook is triggered when comments are listed in the back end. It passes the current record as argument and expects a string as return value. It is available from TYPOlight version 2.8.RC2.

// config.php
$GLOBALS['TL_HOOKS']['listComments'][] = array('MyClass', 'listComments');

// MyClass.php
public function listComments($arrRow)
{
    return '<a href="typolight/main.php?do= … ">' . $arrRow['title'] . '</a>';
}

loadFormField

The loadFormField hook is triggered when a form field is loaded. It passes the widget object, the form ID and the form data as arguments and expects a widget object as return value. It is available from TYPOlight version 2.5.0.

// config.php
$GLOBALS['TL_HOOKS']['loadFormField'][] = array('MyClass', 'loadFormField');

// MyClass.php
public function loadFormField(Widget $objWidget, $strForm, $arrForm)
{
    $objWidget->class = 'myclass';
    return $objWidget;
}

loadLanguageFile

The loadLanguageFile hook is triggered when a language file is loaded. It passes the file name and the language as arguments and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['loadLanguageFile'][] = array('MyClass', 'loadLanguageFile');

// MyClass.php
public function loadLanguageFile($strName, $strLanguage)
{
    // Do something
}

outputBackendTemplate

The outputBackendTemplate hook is triggered when a back end template is printed to the screen. It passes the template content and the template name as arguments and expects the template content as return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['outputBackendTemplate'][] = array('MyClass', 'outputBackendTemplate');

// MyClass.php
public function outputBackendTemplate($strContent, $strTemplate)
{
    if ($strTemplate == 'be_main')
    {
        // Modify output
    }

    return $strContent;
}

outputFrontendTemplate

The outputFrontendTemplate hook is triggered when a front end template is printed to the screen. It passes the template content and the template name as arguments and expects the template content as return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = array('MyClass', 'outputFrontendTemplate');

// MyClass.php
public function outputFrontendTemplate($strContent, $strTemplate)
{
    if ($strTemplate == 'fe_page')
    {
        // Modify output
    }

    return $strContent;
}

parseBackendTemplate

The parseBackendTemplate hook is triggered when a back end template is parsed. It passes the template content and the template name as arguments and expects the template content as return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['parseBackendTemplate'][] = array('MyClass', 'parseBackendTemplate');

// MyClass.php
public function parseBackendTemplate($strContent, $strTemplate)
{
    if ($strTemplate == 'be_main')
    {
        // Modify output
    }

    return $strContent;
}

parseFrontendTemplate

The parseFrontendTemplate hook is triggered when a front end template is parsed. It passes the template content and the template name as arguments and expects the template content as return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = array('MyClass', 'parseFrontendTemplate');

// MyClass.php
public function parseFrontendTemplate($strContent, $strTemplate)
{
    if ($strTemplate == 'ce_text')
    {
        // Modify output
    }

    return $strContent;
}

postDownload

The postDownload hook is triggered after a file has been downloaded with the download(s) element. It passes the file name as argument and does not expect a return value. It is available from TYPOlight version 2.4.6.

// config.php
$GLOBALS['TL_HOOKS']['postDownload'][] = array('MyClass', 'postDownload');

// MyClass.php
public function postDownload($strFile)
{
    // Do something
}

postLogin

The postLogin hook is triggered after a user has logged into the front end. It passes the user object as argument and does not expect a return value. It is available from TYPOlight version 2.4.3.

// config.php
$GLOBALS['TL_HOOKS']['postLogin'][] = array('MyClass', 'postLogin');

// MyClass.php
public function postLogin($objUser)
{
    // Do something
}

postLogout

The postLogout hook is triggered after a user has logged out from the front end. It passes the user object as argument and does not expect a return value. It is available from TYPOlight version 2.4.3.

// config.php
$GLOBALS['TL_HOOKS']['postLogout'][] = array('MyClass', 'postLogout');

// MyClass.php
public function postLogout(Database_Result $objUser)
{
    // Do something
}

postUpload

The postUpload hook is triggered after a user has uploaded one or more file in the back end. It passes an array of filenames as argument and does not expect a return value. It is available from TYPOlight version 2.6.4.

// config.php
$GLOBALS['TL_HOOKS']['postUpload'][] = array('MyClass', 'postUpload');

// MyClass.php
public function postUpload($arrFiles)
{
    // Do something
}

printArticleAsPdf

The printArticleAsPdf hook is triggered when an article is exported as PDF. It passes the article text and the article object as arguments and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['printArticleAsPdf'][] = array('MyClass', 'printArticleAsPdf');

// MyClass.php
public function printArticleAsPdf($strArticle, Database_Result $objArticle)
{
    // Do something
    exit;
}

processFormData

The processFormData hook is triggered after a form has been submitted. It passes the form data array, the Data Container Array and the files array as arguments and does not expect a return value. It is available from TYPOlight version 2.4.4.

// config.php
$GLOBALS['TL_HOOKS']['processFormData'][] = array('MyClass', 'processFormData');

// MyClass.php
public function processFormData($arrPost, $arrForm, $arrFiles)
{
    // Do something
}

removeOldFeeds

The removeOldFeeds hook is triggered when old XML files are being removed from the TYPOlight directory. It does not pass an argument and expects an array of file names to preserve as return value. It is available from TYPOlight version 2.5.8.

// config.php
$GLOBALS['TL_HOOKS']['removeOldFeeds'][] = array('MyClass', 'removeOldFeeds');

// MyClass.php
public function removeOldFeeds()
{
    return array('custom.xml');
}

removeRecipient

The removeRecipient hook is triggered when a newsletter recipient is removed. It passes the e-mail address and the channel IDs as argument and does not expect a return value. It is available from TYPOlight version 2.8.RC1.

// config.php
$GLOBALS['TL_HOOKS']['removeRecipient'][] = array('MyClass', 'removeRecipient');

// MyClass.php
public function removeRecipient($strEmail, $arrChannels)
{
    // Do something
}

replaceInsertTags

The replaceInsertTags hook is triggered when an unknown insert tag is found. It passes the insert tag as argument and expects the replacement value or false as return value. It is available from TYPOlight version 2.6.0.

// config.php
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('MyClass', 'replaceInsertTags');

// MyClass.php
public function replaceInsertTags($strTag)
{
    if ($strTag == 'mytag')
    {
        return 'mytag replacement';
    }

    return false;
}

reviseTable

The reviseTable hook is triggered when TYPOlight removes orphan records from a table. It passes the name of the current table, the IDs of all new records, the name of the parent table and the names of all child tables as arguments and does expect a boolean return value (returning true will cause the current page to be reloaded). It is available from TYPOlight version 2.6.4.

// config.php
$GLOBALS['TL_HOOKS']['reviseTable'][] = array('MyClass', 'reviseTable');

// MyClass.php
public function reviseTable($table, $new_records, $parent_table, $child_tables)
{
    // Do something
}

setNewPassword

The setNewPassword hook is triggered after a new password has been set. It passes the user object and the encrypted password as arguments and does not expect a return value. It is available from TYPOlight version 2.2.3.

// config.php
$GLOBALS['TL_HOOKS']['setNewPassword'][] = array('MyClass', 'setNewPassword');

// MyClass.php
public function setNewPassword(User $objUser, $strPassword)
{
    // Do something
}

validateFormField

The validateFormField hook is triggered when a form field is submitted. It passes the widget object and the form ID as arguments and expects a widget object as return value. It is available from TYPOlight version 2.5.0.

// config.php
$GLOBALS['TL_HOOKS']['validateFormField'][] = array('MyClass', 'validateFormField');

// MyClass.php
public function validateFormField(Widget $objWidget, $intId)
{
    if ($objWidget instanceof FormPassword)
    {
        // Do something
    }

    return $objWidget;
}

Add a comment