0

I have a content type with a boolean field, using radio buttons for day and night.

I would like to create a view with a contextual filter, which shows "day content" between 8:00 am - 8:00 pm and "night content" between 8:00 pm and 8:00 am.

Which is the best way for creating the contextual filter or view?

Or if you'd have a better suggestion, I could change my content type and fields too.

2 Answers 2

2

your approach is correct.

You can do one thing..Just create a view and contextual filter in view for radio field. use hook_views_pre_view() hook to add arguments.

function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'myview' && $display_id == 'page_1') {
    $current_time = date('H'); //24hrs format
    if ($current_time >= 8 && $current_time <= 20) {
      $args[] = "value of day radio button";
    }
    else {
      $args[] = "value of night radio button";
    }
  }
}

Hope this will help

6
  • Just to be clear, do not use the PHP code in a contextual filter. Modify the contextual filter argument through a hook.
    – sareed
    May 12, 2017 at 14:20
  • Okay..That's a good approach.. you can use hook_views_query_alter() hook. and set where clause for that field. by calculating above code. Let me alter my previous comment.
    – Milind
    May 12, 2017 at 14:27
  • While I haven't tested it, I was always under the impression the hook_views_query_alter was very heavy. I personally use hook_views_pre_view to change the args of a view, although I am sure there are at least a couple other options. Please correct me if my assumption is incorrect.
    – sareed
    May 12, 2017 at 14:29
  • Ya..right you can use hook_views_pre_view hook for this..there are other hooks which can be used..such as hook_views_query_alter. But for this case you can use hook_views_pre_view
    – Milind
    May 12, 2017 at 14:38
  • I have tried the code below: function hook_views_pre_view(&$view, &$display_id, &$args) { if ($view->name == 'home_featured_content' && $display_id == 'panel_pane_1') { $current_time = date('H'); //24hrs format if ($current_time >= 8 && $current_time <= 20) { $args[] = "1"; }else { $args[] = "0"; } } } When I try to preview with "1" value as contextual filter, it shows me Day Content, but when I try "0", no result returns. And if I try "Night" as contextual filter, it shows me Night Content. How should I change the code? May 12, 2017 at 17:26
0

Interesting idea.

First off however, I think you should use a date(time) field instead of the boolean. You can then use the between operators for your filter. I haven't used the date field as a contextual filter though so I will double check that in a few.

The other option, if necessary, would be to do your check on time() in a hook_views_pre_view and change your $view-args to the corresponding boolean (make sure to change the right element of the array if you have multiple contextual filters).

I would only use the second option if the date contextual filter is not an option for you.

Your Answer

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

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