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!

Adding the Icon for Menu from page media property

From typo3 6.2.x, please use the following code  to generate the icon for menus from its page’s media property.

lib.menu = HMENU
lib.menu {
entryLevel = 1
expAll = 1
1 = TMENU
1{
wrap = <ul class=”leftmenu”> | </ul>
expAll = 1
NO = 1
NO{
before.wrap = <li>|
stdWrap.cObject = COA
stdWrap.cObject {
10 = FILES
10 {
# Get the images related to the current page
references {
table = pages
fieldName = media
}
# Render each image and wrap it as appropriate
renderObj = TEXT
renderObj {
typolink {
parameter.data = file:current:publicUrl
forceAbsoluteUrl = 1
returnLast = url
}
wrap = |,
}
stdWrap {
# Take only the first image if several are defined
listNum = 0
# Use default image if none is available
ifEmpty.cObject = TEXT
ifEmpty.cObject.typolink {
parameter = path_to_images/logo.png
forceAbsoluteUrl = 1
returnLast = url
}
wrap = <img src=”|” title=”image” />
}
}

20 = TEXT
20.field = title
20.wrap = <span>|</span>
}
after.wrap = |</li>
}
ACT < .NO
ACT.before.wrap = <li class=”active”> |

CUR < .NO
CUR.before.wrap = <li class=”active”> |
}
}

Store a picture in FAL, and retrieve file name using standard sql statement

Scenario: store a picture in FAL, and retrieve file name using standard sql statement
A) TCA declaration is as follows:
‘news_picture’ => array(
‘exclude’ => 0,
‘label’ => ‘LLL:EXT:mynews/Resources/Private/Language/Database.xlf:tx_mynews.news_picture’,
‘config’ => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
‘news_picture’,
array(
‘appearance’ => array(
‘collapseAll’ => TRUE
),
‘maxitems’ => 1,
)
)
),
B) An SQL example:
SELECT * FROM (
SELECT 
tx_mynews.uid as news_uid,
tx_mynews.news_title,
tx_mynews.news_subtitle,
`sys_file`.name AS filename,
`sys_file`.uid AS fileuid,
`sys_file`.identifier
FROM `tx_mynews` 
LEFT JOIN `sys_file_reference` ON 
`tx_mynews`.`uid` = `sys_file_reference`.`uid_foreign`
AND 
 `sys_file_reference`.`tablenames` = ‘tx_mynews’ 
 AND 
 `sys_file_reference`.`fieldname` = ‘news_picture’
 AND 
 `sys_file_reference`.`table_local` = ‘sys_file’ AND
 `sys_file_reference`.`deleted` = 0
LEFT JOIN `sys_file` 
ON
`sys_file_reference`.`uid_local` = `sys_file`.`uid`)
AS docs where docs.filename <> ”
ORDER BY news_title
LIMIT 100

This snippet informs how to add or allow custom tags in RTE

PAGE TS CONFIG AS FOLLOWS:

# Allow embed, parm, object and iframe tags, also see RTE parser code added in config.ts
RTE.default.proc {
allowTags := addToList(object,param,embed,iframe)
allowTagsOutside := addToList(object,embed,iframe)
entryHTMLparser_db.allowTags < RTE.default.proc.allowTags
}

TYPOSCRIPT SETUP SHOULD BE AS FOLLOWS

# Allow embed, parm, object and iframe tags, also see RTE setup added in rte.ts
lib.parseFunc_RTE.allowTags := addToList(object,param,embed,iframe)