4

I want to filter my view like

field_opening_hour_start_time > <?php date("g"); ?>

How can I make this happen? The filter criteria does not let me to use PHP and the contextual filter does not let me to use less than (or greater than), but only equal (=) comparison. Is there a way around this? Or a way to use PHP in filter criteria box?

edit. I am using Office hours module

3 Answers 3

5

Have you tried using the regular filter (granularity of hour) and selecting the 'relative date' instead of the hard-coded date? You could use 'now' to do a greater than or or equal to the current time, which is the same as

Screenshot attached:

enter image description here

2
  • And in this case the problem is that the time value is seen as an integer and I don't know how I could modify it to look like time - that would probably be the perfect scenario.
    – Erno
    Commented Jun 28, 2012 at 7:21
  • Didn't think about this but solved my issue Commented Aug 18, 2017 at 17:59
5

You could use hook_views_query_alter() for this. Look example. Use print_r() or devel functions such as dpm() to view how your field is called.

/**
 * Implements hook_views_query_alter()
 */
function occ_search_views_query_alter(&$view, &$query) {
  if ($view->name == 'restaurants') {
    if (isset($query->where[0])) {
      if (trim($query->where[0]['conditions'][0]['field']) == 'field_data_field_max_group_size.field_max_group_size_value = :field_data_field_max_group_size_field_max_group_size_value') {
        // Use '>=' condition instead of '='
        $query->where[0]['conditions'][0]['field'] = 'field_data_field_max_group_size.field_max_group_size_value >= :field_data_field_max_group_size_field_max_group_size_value';
      }
    }
  }
}
2

you could use this sandbox project views arguments in filter. It permit to use arguments in filter criteria and then all the operators of the filter criteria with the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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