Search code examples
typo3extbasetypo3-6.2.x

How to deal with type "Time*" (set in the Extension Builder) in Controller and View of Extbase extension


I created the entity Appointment in my extension builder.
Each Appointment is happening on a certain day and has a start time and an end time.
It never goes beyond a day so I only need one date.
That's why I added those 3 properties to my model in the extension builder:
enter image description here

When creating a new appointment in the frontend I now know how to deal with the date format but no idea how to convert the time to the fitting Integer as the extension builder set it in my model. enter image description here
When trying to create a new appointment I get this error:

An error occurred while trying to call vendor\extname\Controller\AppointmentController->createAction()

newAppointment.startTime:
    "09:35" is no integer.
    The given subject was NULL.
newAppointment.endTime:
    "10:35" is no integer.

In my controller I have this function which works for the Date:

public function initializeAction() {
        if ($this->arguments->hasArgument('newAppointment')) {
            $this->arguments->getArgument('newAppointment')->getPropertyMappingConfiguration()->forProperty('startDate')->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,'d.m.Y');
        }
    }

I tried adding:

$this->arguments->getArgument('newAppointment')->getPropertyMappingConfiguration()->forProperty('startTime')->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\IntegerConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\IntegerConverter);

but that was a fail as it says:

Fatal error: Undefined constant 'TYPO3\CMS\Extbase\Property\TypeConverter\IntegerConverter' in ...AppointmentController...

I even checked that path and the IntegerConverter is there...
Not sure if I'm supposed to use it in this case or if there's some other simple way to do this...?

And also once this works, how would I display it - currently for the date I'm doing:

{appointment.startDate->f:format.date(format:'d.m.Y')}

but will I be able to do this with my Time Integer?:

{appointment.startTime->f:format.date(format:'H:i')}

Solution

  • The second argument of setTypeConverterOption doesn't expect a class, but a constant from this class. Since the IntegerConverter has no options, this is meaningless anyway. But this clears up one of the errors.

    You can use time fields (internally handled as integer, eval time in TCA), but there's some manual work needed.

    At first you need a converter that determines the timestamp from a given time. You can find an example here.

    Then, in your initializeCreateAction and initializeUpdateAction use the converter to convert the given DateTime from the frontend to the integer to be saved in the DB:

    $propertyMappingConfiguration->forProperty('fromTime')->setTypeConverter($this->objectManager->get('My\\Extension\\Property\\TypeConverter\\TimestampConverter'))->setTypeConverterOption('My\\Extension\\Property\\TypeConverter\\TimestampConverter', \My\\Extension\Property\TypeConverter\TimestampConverter::CONFIGURATION_DATE_FORMAT, 'H:i');
    

    Then you should be fine saving the time to the integer field.