TYPO3 custom session handling.

To implement Shopping cart or shopping basket, we need to store the shopping data in session. Here is the way how we can use simple session handling “Service” to implement it in Extbase extensions.

Below is the php code to handle sessions.  The name of the php file will be “SessionHandler.php” and will be under folder EXT:myext/Classes/Domain/Service. We can identify this from namespace.

<?php

namespace TYPO3\Myext\Domain\Service;

class SessionHandler implements \TYPO3\CMS\Core\SingletonInterface {

public function restoreFromSession() {
$sessionData = unserialize($GLOBALS[‘TSFE’]->fe_user->getKey(‘ses’, ‘basket’));
if (mktime() >= $this->getSessionTimeout()) {
$this->cleanUpSession();
return null;
}
$this->setSessionTimeout();
return $sessionData;
}

public function writeToSession($object) {
$sessionData = serialize($object);
$GLOBALS[‘TSFE’]->fe_user->setKey(‘ses’, ‘basket’, $sessionData);
$GLOBALS[‘TSFE’]->fe_user->storeSessionData();
$this->setSessionTimeout();
return $this;
}

public function cleanUpSession() {
$GLOBALS[‘TSFE’]->fe_user->setKey(‘ses’, ‘basket’, NULL);
$GLOBALS[‘TSFE’]->fe_user->setKey(‘ses’, ‘basketLifetime’, NULL);
$GLOBALS[‘TSFE’]->fe_user->storeSessionData();
return $this;
}

public function setSessionTimeout() {
$GLOBALS[‘TSFE’]->fe_user->setKey(‘ses’, ‘basketLifetime’, mktime() + 600);
$GLOBALS[‘TSFE’]->fe_user->storeSessionData();
return $this;
}

public function getSessionTimeout() {
return $GLOBALS[‘TSFE’]->fe_user->getKey(‘ses’, ‘basketLifetime’);
}
}

So how to use these functions on our controllers ?

First we need to inject it in our controller like this

/**
* @var \TYPO3\Myext\Domain\Service\SessionHandler
* @inject
*/
protected $sessionHandler;

Then we can use below code in controller, for various actions on session.

Let us consider the variable “$basket” has the posted data from the forms. To store that data in session “basket”, below code will help.

$this->sessionHandler->writeToSession($basket);

To retrieve that data from session, we can use

$this->sessionHandler->restoreFromSession();

To cleanup the session,

$this->sessionHandler->cleanUpSession();

In the above php code, we can see that, I have set session timeout at 10 mins (600 secs).

If the user is loggedin, that user session timeout will be handled by

$TYPO3_CONF_VARS[“FE”][“lifetime”] = 600

 

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)