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/

How to add a postUserFunc or parseFunc or binding any userFunction in TYPO3

This is one of the easiest thing that just helps us do almost anything with TYPO3 content.

Well, yes, still the basic stuff we need to find out is how to get this done.

Generally you might have seem in extensions that they use stdWrap, and consider you wish to process the content after the stdWrap is applied.

ex:
After generating a menu, after a news text is displayed from tt_news etc…

As with all my posts, I just love to keep thngs short, so I will just explain this with relation to a tt_news example:

Step 1: Find out where you can bind your user function:

I was in need of processing the news content to strip some special characters from word, so this is what I did.

I could also have used the plugin.tt_news.general_stdWrap.postUserFunc, I however did not wish to touch each field, as I only needed some selected fields to be affected.
Thus, on looking into tt_news/pi/class.tx_ttnews.php I found that there is a config I can make use of that is “itemMarkerArrayFunc”.

Step 2 : Write your user function.

NOTE 1:
Write your userFunc within standard PHP tags, and save it in fileadmin, or EXT folder.
ex:

<?php
//code here
?>

NOTE 2:
Makse sure to understand what type of data is available to us, well sometimes it might be an array as in this case.

NOTE 3:
Your function name should start with “user_

NOTE 4:
Your function declaration should have enough arguments, if not you might be in a surprise.
Generally if you are using parseFunc of stdWrap your function may look like this:
ex:

function user_ProcessData($content,$config){

// do something with $content
$modifiedContent = $content . ” This is modifed”;
return $modifiedContent;

}

THE USER FUNCTION
In my case the function looks like this

function user_ProcessData($markerArray, $conf) {

$str = $markerArray[‘###NEWS_CONTENT###’];
$str = str_replace(‘###CUSTOMER###’,’Customer Name’,$str);
$markerArray[‘###NEWS_CONTENT###’] = $str ;
return $markerArray;

}

Step 3: Include this file in typo3
This can be done in several ways, and here is one such method:

# Add the following code to the setup section of your System Template record. ( we will usually have setup and constants here in General Tab )

includeLibs.itemMarkerArrayFunc = EXT:customext_libs/userNewsMarkerProcessFunc.php

# customext_libs
– is the name of an extension

Step 4: Add final config, so that tt_news knows we want to make use of a parseFunc / userFunc

plugin.tt_news.itemMarkerArrayFunc = user_ProcessData

Step 5: This should generally work.
If not please make sure you followed everything as per the above seteps!

How to enable Admin Panel in TYPO3

In order to make sure that the admin panel is visible when you are in backend, make sure that the following criteria are met:

1. Make sure you are logged in to the BE of same domain.

In a multi-domain setup, if you are logged in www.domain-a.com, and you try to view www.domain-b, then don’t expect the preview / admin panel to work.
This simply does not work!

2. user and/or group setup:

admPanel=1
admPanel {
enable.edit = 1
enable.edit.hide=1
enable.preview = 1
enable.cache = 0
enable.info = 1
module.edit.forceDisplayFieldIcons = 1
module.edit.forceDisplayIcons = 1
module.edit.forceNoPopup = 0
hide = 0
}

3. root template setup:

config.admPanel=1

How to enable jQuery with powermail OR How to remove mootools or prototype from powermail, and use jQuery validation

In some cases we may wish to retain one single library throughout our site, so powermail does not have issues if we choose mootools or prototype.
If we prefer jQuery, then we have some issues at present, as powermail does not yet allow us to use jQuery built in.
The future version however seem to include this feature, and we are waiting, untill then this might prove useful.

  • Step 1: Set nothing for the libraryToUse contstant (of course in constants section)
    plugin.powermail.libraryToUse =
  • Step 2: Set up a different template path for the formWrap — This is important, will explain later in Step3!
    To do this I will copy the formwrap.html located in typo3conf/ext/powermail/templates/tmpl_formwrap.html to fileadmin/templates and set following constant.
    plugin.powermail.template.formWrap = fileadmin/templates/tmpl_formwrap.html

    NOTE: You may change template even in setup section, check manual or ext_typoscript_setup.txt for details.

  • Step 3: Remove the validation script from formwrap.html that we copied just now.
    If you look at the tempalte file, you can see folloing JavaScript:

    <script type=”text/javascript”>
    function formCallback(result, form) {
    window.status = “valiation callback for form ‘” + form.id + “‘: result = ” + result;
    }
    var valid = new Validation(‘###POWERMAIL_NAME###’, {immediate : true, onFormValidate : formCallback});
    </script>

    Remove the above completely including <script> tags.

  • Step 4: Remove automatic inclusion of JS files using TypoScript. (IMPORTANT: This must appear in the setup section)
    page.includeJS.file31311 >
    page.headerData.31310 >

There you go, by now you should have disabled the output of all java scripts and inclusion of JS Libraries like mootools or prototype.
Now you are free to use any library you need.

NOTE: By now the default validation methods available in powermail are also disabled. Well we can easily use the jquery form validation!

Disable a TYPO3 extension or removing a hook specified by another extension

In some cases we may need to disable some hooks that are defined by another extension. Say for example in a recent case, I had to disable js_css_optimizer when a typo3 FE editor is logged in. This was necessary as feeditadvanced was not working together with js_css_optimizer.

So the moment I needed the services of feeditadvanced, I had to disable js_css_optimizer, so I added the following code:

unset($GLOBALS[‘TYPO3_CONF_VARS’][‘FE’][‘cssCompressHandler’]);

We have to make sure that we do this at correct stage, for example, if the extension we wanted to disable has already finished it’s execution, then we may not get desired result, so we may have to add another hook that executes much before the hook we wanted to remove.