2

I have a content type "event" with start_date and end_date (fields by date module).

A View lists this events and I need a custom exposed filter with a SELECT with 3 options:

  • -All-
  • Today
  • In the next 15 day
  • Next month

With the following comparison:

Today (start_date <= NOW && end_date >= NOW)

In the next 15 days (start_date <= NOW+15day && end_date >= NOW)

Next month (start_date <= $next_month_last_day && end_date >= $next_month_first_day)

What is the best way to go to create the above custom filter?

I have to create a Views custom filter handler that does everything, in this case I can generate html select directly in the handler and expose it by default without pass trough views interface to check "expose this.."? Or is best to FIRST create the above separate filters trough Views interface and then ALTER the form to combine all filters in a unique SELECT and then ALTER the query?

I've already take a look to Easy date selector for Views Exposed Date Filter but grouped filters are not a solution for me because I have to manage 2 dates, and not a single one (post date) as in that example.

I also look at How to customize exposed filter for date field? but I'm not sure if using templates like suggested is a good solution for me or there is a more elegant way to reach the goal.

Also found this module: https://www.drupal.org/project/views_between_dates_filter but doesn't provide the select I need.

Thank you very much for any help.

cases

1
  • I don't think there's a module that can help you specifically as this is a fairly custom use-case. Have you considered building your view in code? Or maybe just altering it in a hook_views_pre_execute()?
    – Darvanen
    Sep 29, 2015 at 2:17

2 Answers 2

3

I've written a custom module exactly for this purpose but haven't released it as it ended up being a little less abstracted than I'd like.

The way you do it is by writing a Views filter handler (which is surprisingly easy). Without giving the whole game away and leaving a little homework for you: please read on. Let me know how you go.

You'll need a line in your .info file.

files[] = inc/views_relative_date_filters.inc

Register it with the Views API in the .module file.

/**
 * Implements hook_veiws_api().
 */
function views_relative_date_filters_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_relative_date_filters') . '/inc'
  );
}

Register the Views handlers inc/views_relative_date_filters.views.inc

function views_relative_date_filters_views_handlers() {
  return array(
  // ...
  );
}

function views_relative_date_filters_views_data() {
  $data = array();
  // ...
  return $data;
}

And finally write the filter itself in inc/views_relative_date_filters.inc

class views_relative_date_filters extends views_handler_filter {
  // You'll need an options form (just use the parent)
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
  }

  // You can use a views_form or an
  function exposed_form(&$form, &$form_state) {
    // Return a Form API select element
    // ... all, this-day, this-fornight, this-month
  }

  // The query to get your results based on your time period
  function query() {
    $this->ensure_my_table();

    // Get the value of the submitted filter
    if (isset($this->value[0])) {
      $value = $this->value[0];
    } else {
      $value = 'all';
    }

    // A switch on the $value
    switch( $value ) {
      case 'all';
        return;
      case 'this-month':
        // Modify your query with where conditions, groups, joins, whatever
        // ...
        break;
      case 'this-fortnight':
        // Modify your query with where conditions, groups, joins, whatever
        // ...
        break;
      case 'this-day':
        // ...
        break;
      case default;
        break;
    }
  }
}
4
  • Does the above code works also for Views 'api' => 3 ?
    – MXT
    Sep 30, 2015 at 12:35
  • Good question! We are using this module with Views 3.8. You can see it in action here. niea.unsw.edu.au/events The code above though is a sample. It isn't a complete working version but is enough to give you the idea about how it should work. Sep 30, 2015 at 20:18
  • @Queenvictoria, It will be great if you provide full same code. Like what is to be under views_relative_date_filters_views_handlers and views_relative_date_filters_views_data Mar 15, 2016 at 11:04
  • I'd really encourage you to learn a little about the Views API as it's extremely powerful and actually really straightforward. Good luck! Mar 16, 2016 at 8:39
0

I finally found the module Composite Views Filter that allows to easily reach the goal.

(Ref topic: https://www.drupal.org/node/2576837)

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.