Search code examples
typo3typo3-11.xtca

TYPO3 - TCA - change the value of one field when another changes


I have TYPO3 11.5.26.
I have a custom entity.
I generated a TCA configuration.

I need to change the value of a field on certain conditions of another value field.

Example:
If "deadline field date" <= today then "State field value" = "open" else "closed"

How can I do that?


Solution

  • The only clean way to achive that is to implement a Scheduler Task. A good way is to use a console command. Here is a tutorial how to implement:

    https://docs.typo3.org/m/typo3/reference-coreapi/11.5/en-us/ApiOverview/CommandControllers/Tutorial.html#console-command-tutorial

    In the execute() method of the command you can then iterate all your custom entities, check the deadline field and change the state field if needed. That would look something like this:

    public function execute()
    {
        $allMyEntities = $myEntityRepository->findAll();
        foreach($allMyEntities as $myEntity)
        {
            if($myEntity->getDealine() <= $today)
            {
                $myEntity->setState("closed");
                $myEntityRepository->update($myEntity);
            }
        }
    }