开发者

Time field in Drupal forms?

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 a开发者_运维百科nd use a drupal form field type that describes the time of day? (Something more elegant than just a textfield with the label "time"?)


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.


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.


There does not seem to be a 'standard' solution for this at the moment, but you might find something useful in the following:

  • There is a CCK Time module for Drupal 5, along with some patches for a Drupal 6 port in a feature request.
  • There is a feature request to allow a 'time only' field within the date module, linking some more related threads.
  • Most interesting for you might be this posting of code snippets. It is a concrete example of what googletorp suggested (+1).

And while not exactly providing a time field, you might also be interested in the duration modules, depending on what you want to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜