typo3: scheduler task with additional fields

I know how to use extbase to create scheduler tasks. What I didn’t know is how to add additional fields to a task. Turns out it kinda works if you do not use an extbase task. Most of this I got from this very good example.

I already had a normal extbase CommandController, thankfully all I had to do was rewrite it a little:

  • the executing function must now be called execute() instead of somethingCommand()
  • I had a __construct function, which seems to be problematic. Leave it out entirely if possible or, if not possible, call parent::__construct()

But lets take a step back. First thing is to define the task in the extension’s ext_localconf.php, and a little differently than with normal extbase tasks:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['Ophi\OphiSomething\Command\SomethingCommandController'] = array(
	'extension' => $_EXTKEY,
	'title' => 'Something Task',
	'description' => 'Do something really important with additional fields',
	'additionalFields' => 'Ophi\OphiSomething\Command\SomethingCommandControllerAdditionalFieldProvider'
);

There is a dedicated class to handle the additinalFields, its called the SomethingCommandControllerAdditionalFieldProvider class. I’m not sure if the corresponding name is mandatory, just didn’t want to spend any more time finding out. The class looks like this:

namespace Ophi\OphiSomething\Command;

class SomethingCommandControllerAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {

    protected $important_something_var;

    public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
        // Initialize selected fields
        if (!isset($taskInfo['important_something_var'])) {
            $taskInfo['important_something_var'] = 'default value';
            if ($parentObject->CMD === 'edit') {
                $taskInfo['important_something_var'] = $task->important_something_var;
            }
        }
        $fieldName = 'tx_scheduler[important_something_var]';
        $fieldId = 'important_something_var';
        $fieldValue = $taskInfo['important_something_var'];
        $fieldHtml = '<input id="' . $fieldId . '" name="' . $fieldName . '" type="text" value="' . htmlspecialchars($fieldValue) . '" />';
        $additionalFields[$fieldId] = array(
            'code' =&gt; $fieldHtml,
            'label' =&gt; 'A very important value',
            'cshKey' =&gt; '_MOD_tools_txschedulerM1',
            'cshLabel' =&gt; $fieldId
        );
        return $additionalFields;
    }

    public function validateAdditionalFields(array &amp;$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
        $string = $submittedData['important_something_var'];
		if(trim($string) == ''){
			$parentObject-&gt;addMessage($GLOBALS['LANG']-&gt;sL('Value must not be empty'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
			return FALSE;		
		}
        return true;
    }

    public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
        $task-&gt;important_something_var = $submittedData['important_something_var'];
    }
}

In validateAdditionalFields custom validations can be added and errors can be thrown, in this case I just made sure the value wasn’t empty. I still haven’t found out what cshKey is, not sure if it needs to have a special name or not. Last but not least the SomethingCommandController:

namespace Ophi\OphiSomething\Command;

class SomethingCommandController extends \TYPO3\CMS\Scheduler\Task\AbstractTask{

    public function execute(){
	//the additional field value
	$theImportantValue = $this->important_something_var; 
		
	//... 
		
        return true;
    }
}

And that’s it. Important: when creating the task, do not select ExtbaseController, the new task should be available as its own option, named as it was named in ext_localconf.php

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.