Aristeidis Karavas
Aristeidis Karavas

Reputation: 1956

TYPO3 TCA make the 'default' value dynamic

The title is rather self explanatory, but what i would like to have is a dynamic default value.

The idea behind it is to get the biggest number from a column in the database and then add one to the result. This result should be saved as the default value.

Lets take for example this code:

$GLOBALS['TCA'][$modelName]['columns']['autojobnumber'] = array(
    'exclude' => true,
    'label' => 'LLL:EXT:path/To/The/LLL:tx_extension_domain_model_job_autojobnumber',
    'config' => [
        'type' => 'input',
        'size' => 10,
        'eval' => 'trim,int',
        'readOnly' =>1,
        'default' => $result,
    ]
);

The SQL looks like this:

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_extension_domain_model_job');
$getBiggestNumber = $queryBuilder
     ->select('autojobnumber')
     ->from('tx_extension_domain_model_job')
     ->groupBy('autojobnumber')
     ->orderBy('autojobnumber', 'DESC')
     ->setMaxResults(1)
     ->execute()
     ->fetchColumn(0);
$result = $getBiggestNumber + 1;

So how can i do that "clean"?

I thought about processCmdmap_preProcess but i dont know how to pass the value to the coorisponding TCA field. Plus i do not get any results on my backend when i use the DebuggerUtility like i get them when i use processDatamap_afterAllOperations after saving the Object.

Can someone point me to the right direction?

Upvotes: 3

Views: 1920

Answers (1)

Sybille Peters
Sybille Peters

Reputation: 3229

I don't think it is supported to create a dynamic default value, see default property of input field.

What you can do however, is to create your own type (use this instead of type="input"). You can use the "user" type. (It might also be possible to create your own renderType for type="input", I never did this, but created custom renderTypes for type= "select").

You can look at the code of InputTextElement, extend that or create your own from scratch.

Example

(slightly modified, from documentation)

ext_localconf.php:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][<current timestamp>] = [
    'nodeName' => 'customInputField',
    'priority' => 40,
    'class' => \T3docs\Examples\Form\Element\CustomInputElement::class,
];

CustomInputElement

<?php
declare(strict_types = 1);
namespace Myvendor\MyExtension\Backend\FormEngine\Element\CustomInputElement;

use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;

// extend from AbstractFormElement
// alternatively, extend from existing Type and extend it.
class CustomInputElement extends AbstractFormElement
{
   public function render():array
   {
        $resultArray = $this->initializeResultArray();

        // add some HTML 
        $resultArray['html'] = 'something ...';

        // ... see docs + core for more info what you can set here!


        return $resultArray;
   }
}

Upvotes: 1

Related Questions