TYPO3 RTE parseFunc to customize links.

Here I had a requirement to add a button style to RTE link. By adding the link class, I was able to add some class to link and able to generate the links like below.

<a herf=”#” class=”btn”>My link</a>

But according the my designer, the link should have a <span> tag inside the <a> tag. It should look like below.

<a herf=”#” class=”btn”><span>My link</span></a>

So, I have used a RTE parseFunc to do this.

Below are the steps.

1. Add the below typoscript in TS template setup.

includeLibs.userFunc= EXT:mycustom_config/Classes/UserFunction/Addspan.php
tt_content.text.20.parseFunc.tags.link {
postUserFunc= user_addSpan->addSpan
postUserFunc{
class = btn
ATagTitle.field = title
}
}

If we use namespaces, we no need to include the php file by using “includeLibs”. In that case, we can call the userfunction directly like below

postUserFunc= Namespace\Myext\UserFunctions\Addspan->addSpan

2.  On file Addspan.php we need to add below code.

<?php
class user_addSpan{
/*
* Adds span inside <a> tag, if the link class ‘btn’ is selected
*/
function addSpan($content,$conf) {
$class = $conf[‘class’];
if (preg_match(‘/class\=”(.*’. $class .’.*)”/i’, $content, $res)) {
$content = preg_replace(‘@>(.*)</a>@i’, ‘><span>$1</span></a>’, $content);
}
return $content;
}
}
?>

In this php function, I am checking for link class “btn” and adding a <span> tag.

That’s it. We can use this technique for generating Record links too, In that case we can generate the typolink to record detail view and replace “href”.

Cheers!

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!