The field handler views_handler_field_date only works with unix timestamp fields.

With a small tweak it could also work on SQL datetime fields. This would allow contrib modules that have data in this format (such as https://drupal.org/project/data, potentially) to make use of this handler.

(Note that while the Date module's Views support isn't suitable for this purpose -- it in fact doesn't have a field handler, as it uses the default Views field handler for Field API fields.)

Comments

joachim’s picture

Status: Active » Needs review
StatusFileSize
new1.06 KB

Here's a patch.

The intention here is that contrib modules could declare this handler in their hook_views_data() for table fields that hold MySQL datetime data.

jblumenfeld’s picture

This seems useful, but for completeness it would probably also need something to handle sort and filter?

joachim’s picture

It's a few weeks since I worked on this, but I seem to remember that sort and filter work as they are.

Andre-B’s picture

If I understand correctly: currently a contrib module would have to extend the views_handler_field_date and override the render method to provide datetime support?

saw a reference here: http://drupal.stackexchange.com/questions/34261/creating-a-sortable-field

lucyp’s picture

The patch works for me, and I really appreciate this change. Now I'm able to extend that handler in all kinds of useful ways. I vote that it gets incorporated into Views.

JulienThomas’s picture

Instead of patching date field handler, why not creating a specific field handler?

as even the patch suggest, the patch itself makes a big assumption

+ // If the value isn't numeric, assume it's an SQL DATETIME.

So why not going toward a field handler such as

class module_handler_field_datetime extends views_handler_field_date {
 function render($values) {
     $value = $this->get_value($values);
return parent::render(strtotime($value)); 
 }
}

that way, we still use views_handler_field_date and add a specific strtotime for SQL datatime fields plugged in from hooks_view_data.

obviously, you need to add the following line to module.info

files[] = handlers/module_handler_field_datetime.inc

berliner’s picture

parent::render expects the full $values object, so this would be better:

class module_field_datetime extends views_handler_field_date {
 function render($values) {
   $values->{$this->field_alias} = strtotime($this->get_value($values));
   return parent::render($values);
 }
}
caminadaf’s picture

Status: Needs review » Reviewed & tested by the community

I'm sorry @JulienThomas, but I disagree with you. It is not pratical at all to have different field handlers for each "date" sql column.

Also, is it really such a big assumption to try to turn the value into timestamp if it isnt a numeric? Is there any possibility of this assumption to break something?

IMO this is such a simple change that can help so many people without any impact on the current functionality.

I believe this should be patched.

natanmoraes’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.08 KB

Reworked the patch with latest dev code.

mustanggb’s picture

Status: Needs review » Reviewed & tested by the community

#9 is just a straightforward re-roll, so back to RTBC as per #8.

pythagory’s picture

StatusFileSize
new956 bytes

#9 no longer applying for me after updating to 3.20, rerolled patch attached.

natanmoraes’s picture

#11 also works for 3.21

Any chance this could be added to the next release? It's RTBC for 2 years already

damienmckenna’s picture

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.