4

I'm using Drupal 8. I created a new content type (title, body, image, date). The date is a timestamp field.

I used the devel module and the Realistic Dummy Content module to generate a test content for my view test.

I created my test view and chose the date as an exposed filter.

Here is my configuration: enter image description here

And this is my view: enter image description here

When I apply my filter, I want to say all contents having a date greater than my input:

If I test with 01/31/2018 - 02:54, nothing was changed!

But If I test with the unix value: 1538323253, my filter works well!

Any idea please to make the filter with human readable value?

  • What happens if you reproduce the same thing with a Date field? – 4k4 Jan 16 '19 at 10:19
  • 2
    I'd expect it needs a UNIX timestamp as input... – leymannx Jan 16 '19 at 10:56
  • 1
    @leymannx, yes thank you! if I test with a value as 1538323253 the filter works well, but it's not what I need! – A.Sana Jan 16 '19 at 11:16
  • 1
    I don't think it's possible. I'd go around the problem, by adding a date field, either with JS or a template override kind of thing, and hide the timestamp visually. I'd then use JS to update the timestamp value based on the date field onChange/onBlur. – beltouche Mar 6 at 1:07
  • 1
    You need to create a custom exposed view filter to create our condition as you wish and use a field date in form exposed, here you can find a tutorial with a recipe to create your own exposed view filter : axelerant.com/resources/team-blog/… – Christophe CARON Mar 6 at 10:58
2

I ran into this issue on D7. I enabled the views query and noticed that the values were being passed as human date to the query rather than timestamp.

So what I did was use hook_views_query_alter and converted them to timestamp.

Example code (D7 code)

function mymodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'list_employers') {
    foreach ($query->where[1]['conditions'] as $con_key => $con_val) {
      if ((strpos($con_val['field'], 'node.created = ') !== FALSE) &&
        empty($con_val['value'])) {
        $date = $con_val['field'];
        $date = str_replace('node.created = ', '', $date);
        $date = date('m/d/Y', $date);
        $start = strtotime($date . ' 12:00 AM'); // convert to timestamp
        $end = strtotime($date . ' 11:59 PM'); // convert to timestamp
        unset($query->where[1]['conditions'][$con_key]['value']); // remove default condition
        $query->where[1]['conditions'][$con_key]['field'] = 'node.created';
        $query->where[1]['conditions'][$con_key]['value'][0] = $start;
        $query->where[1]['conditions'][$con_key]['value'][1] = $end;
        $query->where[1]['conditions'][$con_key]['operator'] = 'BETWEEN';
      }
    }
  }
}
| improve this answer | |
2
+150

As mentioned before you have to enter a UNIX time into the text field.

If you want to use a date picker in the exposed filter in Drupal 8 have a look at this issue (currently there is a patch for that):

https://www.drupal.org/project/drupal/issues/2648950

Drupal 8 Date picker

| improve this answer | |
  • 1
    About time, I was shaking my head when I had to come up with my solution. – No Sssweat Mar 6 at 13:53
-3

https://www.drupal.org/project/date_popup

This will add the native HTML 5 date popup widget to all date fields in views filters.

But this module won't work with timestamp fields.

| improve this answer | |
  • 1
    OP doesn't use a date field. OP uses a timestamp field. Does Date Popup work with timestamp fields as well? – leymannx Jan 16 '19 at 11:41
  • Oh! Yes, it won't work with timestamp fields. Updating my answer – ktrev Jan 16 '19 at 12:14

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.