TYPO3 Extbase custom viewhelper.

As the name suggest, viewhelper helps to modify the view/output of a fluid template.

Here I am showing how to write a viewhelper. This simple viewhelper returns “Yes” if “1” is passed and else “No”.

1. Create the following file.

myExt/Classes/ViewHelpers/YesnoViewHelper.php

2. Write code in that file

<?php
namespace Vendor\Myext\ViewHelpers;

class YesnoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

/**
* @param integer $myvalue
*/
public function render($myvalue) {
$content = ”;
if($myvalue == 1) {
$content = ‘Yes’;
} else {
$content = ‘No’;
}
return $content;
}
}

?>

3. In fluid template use it like below

{namespace custom=Vendor\Myext\ViewHelpers}

<custom:yesno myvalue=”{value}” />

 

Logout TYPO3 FE user when he enter specified page.

I wrote the following function to logout the FE user having specified usergroup, if he enter the specified page.

Specify the user function

includeLibs.logoutcheck = EXT:myextension/Classes/UserFunctions/UserLogoutCheck.php
page.30 = USER
page.30 {
userFunc = tx_user_logoutcheck->checkPageToLogout
logoutpageRoot = 10,20 #user will be logout if he enters any page under page id 10 and 20
userGroupToLogout = 1 #user having usergroup with uid 1 will be logout
}

On File UserLogoutCheck.php

<?php

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;

class tx_user_logoutcheck{

/**
*
* @param  string      Empty string (no content to process)
* @param  array        TypoScript configuration
* @return void
*/
public function checkPageToLogout($content, $conf) {

if($GLOBALS[‘TSFE’]->fe_user){
$logoutpageRoot = $conf[‘logoutpageRoot’];
$userGroupToLogout = $conf[‘userGroupToLogout’];
$currentUserGroup = $GLOBALS[‘TSFE’]->fe_user->user[‘usergroup’];

if(($userGroupToLogout == $currentUserGroup) && $userGroupToLogout != ”) {
$logoutpageArray = explode(‘,’ ,$logoutpageRoot);
$local_cObj = GeneralUtility::makeInstance(‘TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer’);
$findRootline = $GLOBALS[‘TSFE’]->sys_page->getRootLine(intval($GLOBALS[‘TSFE’]->id));
foreach($findRootline as $key=>$value){
if((in_array($value[‘pid’] , $logoutpageArray)) ||  (in_array($value[‘uid’] , $logoutpageArray))){
$GLOBALS[‘TSFE’]->fe_user->logoff();
$GLOBALS[‘TSFE’]->loginUser = 0;

//Redirect to same page: oterhwise userwill not logout due to cache issue.
$linkconf[‘parameter’] = intval($GLOBALS[‘TSFE’]->id);
$linkconf[‘returnLast’] = ‘url’;
// $linkconf[‘additionalParams’] = ‘&logintype=logout’;
$linkUrl = $local_cObj->typoLink(”,$linkconf);
HttpUtility::redirect($linkUrl);
}
}
}
}
}
}

Make Pibase extension compatible to TYPO3 6.2.x

1) Remove the line starting with “require_once”, that includes TYPO3 core files
For Ex:
require_once(PATH_tslib.’class.tslib_pibase.php’);
require_once(PATH_t3lib.’class.t3lib_tsparser.php’);

But keep the “require_once” line that includes custom class from current extension
For Ex:
require_once (PATH_site.”/typo3conf/ext/myextension/library/class.mylibclass.php”);

2. Download the TYPO3 source, in my case I am referring to TYPO3 6.2.2
Open the file “typo3-6.2.2\sysext\core\Migrations\Code\LegacyClassesForIde.php”.

This file has very important information regarding migrations.

3. Open the pi1.php file of the extension that you are going to update.

4. Replace the class names by referring to LegacyClassesForIde.php of TYPO3 core.
For Ex:
Replace “tslib_pibase” with “\TYPO3\CMS\Frontend\Plugin\AbstractPlugin”.
To do this, search for “tslib_pibase” on “LegacyClassesForIde.php” and replace with the part next to “extends”
“abstract class tslib_pibase extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {}”

So
“class tx_myextension_pi1 extends tslib_pibase {”
becomes
“class tx_myextension_pi1 extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {”

5. Similarly replace “t3lib_div” with “\TYPO3\CMS\Core\Utility\GeneralUtility”.
We can do search and replace all occurrences.

6. Similarly replace “t3lib_extMgm” with “\TYPO3\CMS\Core\Utility\ExtensionManagementUtility”
and repeat this step until all the replacement availabilities are replaced.

7. And for custom class inclusions, replace
require_once (PATH_site.”/typo3conf/ext/myextension/library/class.mylibclass.php”);
with
require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(‘myextension’).’library/class.mylibclass.php’);
“PATH_site” may not work in some cases.

8. Replace
t3lib_div::makeInstance(‘t3lib_TSparser’);
with
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(‘TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser’);
The paths comes in quotes should have double back slashes.

9. Remove the line
t3lib_div::loadTCA(“tt_content”);
that may be there in ext_tables.php
In TYPO3 6.2 version, all TCA are auto loaded. So we need not load again.

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!