TYPO3: Extbase datetime property converter

Some times we define DateTime property for a variable in model and in front end we provide different values.
This leads to an error while creating or updating the object's value.
In such cases we need to convert the object property by using the property mapper.
The following example shows Datetime property convertion.

 

In Calendar.php we have the following lines

 

<?php
    namespace VENDOR\Extname\Domain\Model;

    /**
     * Calendar
     */
    class Calendar extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
        /**
         * Start date
         *
         * @var DateTime
         */
        protected $startdate;
        
        /**
         * Returns the startdate
         *
         * @return DateTime $startdate
         */
        public function getStartdate() {
            return $this->startdate;
        }

        /**
         * Sets the startdate
         *
         * @param DateTime $startdate
         * @return void
         */
        public function setStartdate($startdate) {
            $this->startdate = $startdate;
        }
            
    }
?>

 

In CalendarController.php we have the following

 

<?php
    namespace VENDOR\Extname\Controller;

    /**
     * CalendarController
     */
    class CalendarController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

        /**
         * calendarRepository
         *
         * @var \VENDOR\Extname\Domain\Repository\CalendarRepository
         * @inject
         */
        protected $calendarRepository = NULL;

        
        public function listAction() {

            /*
                .....
            */
        }

        

        /**
         * action new
         *
         * @param \VENDOR\Extname\Domain\Model\Calendar $newCalendar
         * @ignorevalidation $newCalendar
         * @return void
         */
        public function newAction(\VENDOR\Extname\Domain\Model\Calendar $newCalendar = NULL) {
            $this->view->assign('newCalendar', $newCalendar);
        }

        /**
         * action create
         *
         * @param \VENDOR\Extname\Domain\Model\Calendar $newCalendar
         * @return void
         */
        public function createAction(\VENDOR\Extname\Domain\Model\Calendar $newCalendar) {
            // to avoid the error we call the initializeAction
            $this->initializeAction();
            
            $this->addFlashMessage('The object was created.');
            $this->calendarRepository->add($newCalendar);
            $this->redirect('list');
        }

        
        // Here we convert the property mapping using the property mapper
        public function initializeAction() {
            if ($this->arguments->hasArgument('newCalendar')) {
                $this->arguments->getArgument('newCalendar')->getPropertyMappingConfiguration()->forProperty('startdate')->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,'d-m-Y');
            }
        }
        
    }
?>

 

This avoids the error and the object is created or updated even when we provide the string value for "startdate" from frontend.