TYPO3 fluid selectbox viewhelper.

In the below example, the selectbox options are coming from locallang.

‘hotelclass’ => array(
‘exclude’ => 1,
‘label’ => ‘LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_hotels.hotelclass’,
‘config’ => array(
‘type’ => ‘select’,
‘items’ => array(
array(‘LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_hotels.hotelclass.2star’, ‘2’),
array(‘LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_hotels.hotelclass.3star’, ‘3’),
array(‘LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_hotels.hotelclass.5star’, ‘5’),
array(‘LLL:EXT:myext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_hotels.hotelclass.7star’, ‘7’),
),
‘size’ => 1,
‘maxitems’ => 1,
‘eval’ => ”
),
)

To display this select box in FE from, we can use the custom view helper.

To use the viewhelpler follow the following steps.

1. Create the file

myext/Classes/viewHelpers/TcaOptionsViewHelper.php

2. Add the below code

<?php
namespace TYPO3\Myext\ViewHelpers;

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

/**
* local cache for dataMaps
* @var array
*/
protected $dataMaps = array();

/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
* @inject
*/
protected $dataMapper;

/**
* objectManager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @inject
*/
protected $objectManager;

/**
* Is returning select values for a property or propertyPath of a given object or className
* @param mixed $subject The object or className the property belongs to
* @param string $property The property itself
* @return array The select options as array
*/
public function render($subject = NULL, $property = ”) {
if (!is_object($subject) && is_string($subject) && class_exists($subject)) {
$subject= $this->objectManager->get($subject);
}
return self::getSelectOptions($property, $subject);
}

/**
* Is resolving the select values for a property or propertyPath of a given object or className
* @param string $propertyPath The property itself
* @param object $subject The object or className the property belongs to
* @return array The select options as array
*/
private function getSelectOptions($propertyPath, $subject) {
$selectOptions = array();

if (is_object($subject)) {
$propertyPathSegments = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(‘.’, $propertyPath);
$object = clone($subject);
$propertyName = $propertyPath;

// resolve a property path
if (count($propertyPathSegments) > 1) {
foreach($propertyPathSegments as $key => $propertyName) {
$propertyType = $this->dataMapper->getType(get_class($tempObject), $propertyName);
if (strpos($propertyType,’_’)) {
$object = $this->objectManager->create($propertyType);
} else {
break;
}
}
}

$dataMap = self::getDataMap($object);

if ($dataMap == NULL || !$dataMap->isPersistableProperty($propertyName)) {
return $selectOptions;
}

$tableName = $this->dataMapper->convertClassNameToTableName(get_class($object));
$columnName = $dataMap->getColumnMap($propertyName)->getColumnName();

// only convert select items which do not have a DB relation
$columnConfig = $GLOBALS[‘TCA’][$tableName][‘columns’][$columnName][‘config’];
if ($columnConfig[‘type’] == ‘select’ && count($columnConfig[‘items’]) && !$columnConfig[‘foreign_table’]) {
foreach ($columnConfig[‘items’] as $option) {
$selectOptions[$option[1]] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($option[0], $this->controllerContext->getRequest()->getControllerExtensionName());
}
}
}
return $selectOptions;
}

/**
* Resolve the dataMap for the given object
* @param object $object The domain object to get the dataMap for
* @return Tx_Extbase_Persistence_Mapper_DataMap
*/
private function getDataMap($object) {
$class = is_object($object) ? get_class($object) : $object;
if (!isset($this->dataMaps[$class])) {
$this->dataMaps[$class] = $this->dataMapper->getDataMap($class);
}
return $this->dataMaps[$class];
}
}
?>

3. Add the below lines in fluid template, that is intended to show FE form

{namespace custom=TYPO3\Myext\ViewHelpers}

<f:form.select property=”hotelclass” options=”{custom:TcaOptions(property:’hotelclass’,subject:’TYPO3\\Myext\\Domain\\Model\\Hotels’)}” />

4. The output source will be like this.

<select name=”tx_myext_hotels[hotels][hotelclass]”>
<option value=”2″>2 Star</option>
<option value=”3″>3 Star</option>
<option value=”5″>5 Star</option>
<option value=”7″>7 Star</option>
</select>

Thanks to: https://gist.github.com/anonymous/888297

 

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.