Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time field in Drupal forms?

Tags:

drupal

In creating a drupal form, one can easily create a date field.

'#type' => 'date',

But what about a time field? Is there an easy way to create and use a drupal form field type that describes the time of day? (Something more elegant than just a textfield with the label "time"?)

like image 832
Dan Burton Avatar asked Dec 10 '22 18:12

Dan Burton


2 Answers

Drupal 7:

date_popup in the date module makes really nice time fields You can use a form element like:

'#type' => 'date_popup',

'#date_format' => 'g:i a',

and the field will match the format you use. If you don't include date elements, it will just show time.

like image 113
user645475 Avatar answered Jan 10 '23 16:01

user645475


Actually, the date field is nothing more than a textfield with a label. That is on the outside. What you do get when using this field from the date module, is a lot of date validation, to make sure that what the user has typed/selected makes sense. The other thing you get is some different javascript tools to aid the typing of the actual date. But to do this, you need the data module.

Since time is a lot more simple (universal) than a date, you wont need much validation. So all you need, to make it into a fancy time field, is to add some javascript to help out, but this is not really needed.

So the short answer is no, you can't do something like that, but you don't really need to either. You could create your own time field with hook_elements() though if you really wanted to.

like image 35
googletorp Avatar answered Jan 10 '23 16:01

googletorp