0

I have added a date field to the article content type. I am writing a view to display this date. When I add the field I get a formatter drop-down which allows me to select three different date formats "Date & Time", "ago" or "plain". However if I add the article post date I get more options including a "custom" format option. It seems that the handler associated with my date field is 'views_handler_field_field' whereas the handler associated with the 'system added' date field is 'views_handler_field_date'. What is going on here? Why are the two fields treated differently? What do I need to do to get the extra options?

1
  • Have you enabled the "Date Views" module? – Clive Aug 12 '14 at 17:09
1

I dont know why the post date has extra options, it seems that it allows you to create a custom format "on-the-fly". But you can create a custom date format and use it for your date field, and for other date fields as well along with the "Short", "Medium" and "Long" formats.

If you go to: configuration > date and time, you should see the three default ones (Short, medium,long). You can then click the "Formats" tap and then Add format. In the text field you can write a string that provides the format you want. Use the PHP date manual to find the letters that provides the month, date and so on. E.g if you want your format to be something like:

25-03-2014 15:48

Your string would be:

d-m-Y H:i

(Hint - you dont need necessarily to have minutes, you could also just have: d-m-Y)

Save that format and go back to the "Types" tap. Click Add date type, give it a name and then select your format on the list (it is typically at the bottom).

Then head over to your view, click the date field, and under the formatter, select Date and time, and then you should see your newly created date format on the list. Save your view and your custom date should display as the date...

1
  • Thanks that worked. In the meantime I managed to get it to work by writing the function shown below – Lance Holland Aug 13 '14 at 11:02
0
function mymodule_views_data_alter(&$data) {

// loops through fields definitions looking for our date fields // and set the date handler.

foreach($data as $module => &$table) {
    foreach($table as $id => &$field) {
      if ($module == 'field_data_field_test_date2'){
       if (is_array($field) &&
          array_key_exists('field', $field) &&
          is_array($field['field']) &&
          array_key_exists('handler', $field['field']) &&
         ($field['field']['is date'] == TRUE)) {
          $field['field']['handler'] = 'views_handler_field_date';

        }  
      }

    }
  }
}

My date field then acts like the post date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.