Has anyone figured out the 5.7 version of Loader::helper('form/date_time')?

I am using the form/date_time helper in a block and it works well for picking a date.
Example code that I am using:
<?php echo $form->label('tourDate', t('Tour Date')) ?>
<?php echo Loader::helper('form/date_time')->date('tourDate', $tourDate); ?>

I would like to add the ability to select a time like the date/time attribute in composer does.

Also, I know there is a move away from Loader functions, so I am interested in learning how to use the new 5.7 approach for this.

For clarity, I attached the date picker that I am currently getting in my block and the desired date time picker used in composer.

Thank you

2 Attachments

View Replies: View Best Answer
Mainio replied on at Permalink Reply
The new way of loading the helpers is through the IoC contaner:

<?php echo Core::make('helper/form/date_time')->date(...); ?>

And if you want to include the time selector, change the "date" function name to "datetime".
MrKDilkington replied on at Permalink Reply
Using the datetime() method, I am getting a date and time picker, but the picked values are not being stored in the database.

These work as expected, I get the date picker and the values are stored.
<?php echo Core::make('helper/form/date_time')->date('tourDate', $tourDate); ?>

<?php echo Loader::helper('form/date_time')->date('tourDate', $tourDate); ?>

These supply the date time picker, but the picked values aren't being stored.
<?php echo Core::make('helper/form/date_time')->datetime('tourDate', $tourDate); ?>

<?php echo Loader::helper('form/date_time')->datetime('tourDate', $tourDate); ?>
Mainio replied on at Permalink Reply
How are you storing the value?

The DateTime helper/service provides a "translate" method which allows you to fetch the correct selected value when using the date/time selector. You need to use that because the value comes from multiple fields if you include the time selector.

$tourDate = Core::make('helper/form/date_time')->translate('tourDate');
MrKDilkington replied on at Permalink Reply
I am storing the value in a field called "tourDate" in db.xml as type="C" (VARCHAR). It works for fine for just dates.

Your explanation of translate() makes sense, but I don't know how to use the example code you provided to create the date time picker or save the $tourDate.

I appreciate your patience. I am new to concrete5 and what I know is based on experimenting with exists blocks.
MrKDilkington replied on at Permalink Reply
I think I have an idea where some of the confusion comes from. The test block I am using was based on a block without save arguments. I've never used save arguments before.

Here is the controller:

Here is the form:

Based on what I linked to, is the issue that I need save arguments in my controller for $tourDate? Questions like these will soon be cleared up when the block documentation comes out.
Mainio replied on at Permalink Best Answer Reply
OK, thanks for the code samples, clarifies this quite a bit.

Yes, you are correct that you need to override the save() method:
public function save($data) {
    $data['tourDate'] = Core::make('helper/form/date_time')->translate('tourDate');

Also another point, in your db.xml you should use the 'T' type for date+time and 'D' type for only the date as described in the ADOdb docs:

While it might also work with the 'C' type it does not mean that it's correct. You should always use the correct type of a db field, that's what they are for. If you learn it the 'wrong' way the first time, you end up doing it that way also in the future.

Believe me, it will also help you in the future when you're doing something more complex.
MrKDilkington replied on at Permalink Reply
Thank you, Mainio.

That was a huge help and I will learn from it. I also bookmarked that page.

Excellent advice on using the right data type. I was originally using T, but changed it to C - thinking that maybe the format being sent needed to be converted first.