Call TYPO3 plugin from typeNum

For example, If our plugin is intended to generate some xml sitemaps and we want to call that plugin like this http://www.mysite.com/?type=1234, We can follow the following steps.

1. In extension setup add the following typoscript

plugin.tx_myext {}
#After this

myext = PAGE
myext {
typeNum = 1234
10 = USER_INT
10 {
userFunc = tx_extbase_core_bootstrap->run
pluginName = MypluginName
extensionName = myExtName
controller = MyControllerName
action = actionName
}
config {
doctype = xhtml_basic
xhtml_cleaning = all
disableAllHeaderCode = 1
additionalHeaders = Content-type:text/xml
xhtml_cleaning = 0
admPanel = 0
debug = 0
}
}

Now, install the extension and if we call http://www.mysite.com/?type=1234, we can see direct xml structure, if everything is correctly written in the extension.

TYPO3 CommandController In Scheduler Task

This command controllers are used for scheduler tasks.
The major use of commandController is, we can get the extensions/plugin settings for manipulations. But in actual scheduler tasks, this is difficult.

In below command controller, I am showing how to update the status of the custom database table on every day automatically using Schedular Task.

Here I am getting the record Id to update, from settings.

1. Create the following file

myExt/Classes/Command/StatusUpdateCommandController.php

2. Write the below code in that file

<?php
namespace Vendor\Myext\Command;

use TYPO3\CMS\Core\Utility\GeneralUtility;

class StatusUpdateCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController {
/**
* @var TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
* @inject
*/
protected $configurationManager;

/**
* The settings.
* @var array
*/
protected $settings = array();

/**
* Initialize the controller.
*/
protected function initializeCommand() {
// get settings
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
‘MyextName’, ‘mypluginName’
);

}

/**
* Main function
*@return void
*/

public function StatusUpdateCommand() {
// settings
$this->initializeCommand();

$mystatus = 1;
$this->statusUpdate($mystatus, $this->settings[‘updateRecordId’]);

return true;
}

/*
* Update status
*/
private function statusUpdate($mystatus,$recordUid) {
$statusUpdateArray = array(
‘mystatus’ => $mystatus,
‘tstamp’ => $GLOBALS[‘EXEC_TIME’]
);
$GLOBALS[‘TYPO3_DB’]->exec_UPDATEquery(‘tx_myext_domain_model_mytable’,’uid=’.$recordUid, $statusUpdateArray);
}
}
?>

3. In setup file “Myext\Configuration\TypoScript\setup.txt”

plugin.tx_myext {
settings {
updateRecordId = 1
}
}
module.tx_myext < plugin.tx_myext

4. Register this command controller in local_conf.php of the extension.

$GLOBALS[‘TYPO3_CONF_VARS’][‘SC_OPTIONS’][‘extbase’][‘commandControllers’][] = ‘Vendor\Myext\Command\StatusUpdateCommandController’;

5. Now we can get this command controller task under the “extbase / Extbase CommandController Task” menu entry of “Scheduler task”.

cmdcntrl1

 

cmdcntrl2

That’s it.

 

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.