TYPO3 Extbase call action through ajax.

Below are the steps to call controller action from ajax.

1. Add the following typoscript on extension setup.

plugin.tx_extname {}
#After this
actionname = PAGE
actionname {
typeNum = 1234
config {
disableAllHeaderCode = 1
additionalHeaders = Content-type:application/html|Cache-Control:no-cache, must-revalidate, max-age=0|Pragma:no-cache
xhtml_cleaning = 0
admPanel = 0
}

10 < tt_content.list.20.extname_controllername
}

3. In controller

/**
* action actionname
*
* @return void
*/
public function actionnameAction() {
return ‘test ajax content’;
}

2. In fluid template

<f:link.action action=”actionname” additionalParams=”{type:’1234′,no_cache:’1′}” noCacheHash=”1″ class=”test”>
Ajax click
</f:link.action>
<div id=”ajax_content”></div>
<script type=”text/javascript”>
$(document).ready(function(){
$(‘.test’).click(function() {
var href = $(this).attr(‘href’);
$.ajax({
url: href,
cache: false,
success: function(htmldata){
$(“#ajax_content”).html(htmldata);
}
});
return false;
});
});
</script>

If everything is correct, it should work.

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);
}
}
}
}
}
}