Hi Guys,

This looks very promising for me, but I can't seem to get it to work with two filters.

I have an event content type, with a start and end field. If an exhibition starts on Jan 1st and ends on Dec 31st, and I use one exposed filter, all seems fine. For example, if I choose, Apr 1st, I see all events on that date, including events that cross over that date, such as an exhibition that starts on Jan 1st and ends on Dec 31st.

However, if I put in two exposed filters so users can search for all events from Apr 1st to Apr 16th, I seem to be missing something.

Could you point me in the right direction please? I'll gladly write up your response as a documentation page for the Drupal.org Handbook, which you can also include then in the README.txt file for the module.

Thanks a lot.

Comments

phelix’s picture

Any luck getting this to work? I am having the same problem. When using in between on dates it does not display anything.

markconroy’s picture

No luck yet. I've moved on to another project now, but would love to get this working. For the moment, client is happy with one filter.

cmonnow’s picture

Hi,

I don't know why but for me the whole views plug-in system is strangely intimidating.

In the meantime, perhaps you can hook (or hack) into what already exists in the Date module without the use of this module at all. Setting an exposed "event start" date filter with day granularity and a preset "in between" filter would give you all the variables you need except event "end" date (easily fixed by adding a "2" to the field name) to establish a formula that meets your requirements - event start date field name, event end date field name, exposed start date variable can be used as an "earliest day" and exposed end date variable can be re-purposed as the "latest day" (rather than being the "latest start date").

It's a bit of a headache wrapping one's head around the precise formula that would work (e.g. when to use ">=" vs ">") but I believe this is the general idea:

(Event start date between exposed start (>=) AND exposed finish (<=) (where exposed start = exposed finish where 1 specific day wanted)
OR (Event end date between exposed start (>) AND exposed finish (<=))
OR (Event start date less than or equal to (<=) exposed start date AND event end date more than (>) exposed end date)

If you haven't used a MODULENAME_views_query_alter function before,

"This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified."

So in your custom_example.module, you'll have this:

/**
 * Implementation of hook_views_api().
 */
function custom_example_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'custom_example'),
  );
}

And in your custom_example.views.inc something like this to hack your specific view type:

function custom_example_views_query_alter(&$view, &$query) {
        //BTW, can use dpm($view->name) to find $view->name
	if ($view->name == 'event') {
                //BTW, can use dpm($query) to find keys/values in "where" parameters
		foreach ($query->where as &$condition_group) {
			foreach ($condition_group['conditions'] as $key=>&$condition) {
                               //find field string (in this case, formula) that contains one of the expected exposed field names
				if (stripos($condition['field'], 'field_data_field_event_date_range_field_event_date_range_value1') !== FALSE) {
                                        //Get the event start time database reference
					$start_time = stristr($condition['field'], ' >=', true);
                                        //Replace the database reference to start date (...value) with end date (...value2)
					$end_time = str_replace("field_data_field_event_date_range.field_event_date_range_value","field_data_field_event_date.field_event_date_range_value2", $start_time);

					$condition['field'] = "(" . $start_time . " >= :field_data_field_event_date_range_field_event_date_range_value AND " . $start_time . " <= :field_data_field_event_date_range_field_event_date_range_value1)";
					$condition['field'] .= " OR (" . $end_time . " > :field_data_field_event_date_range_field_event_date_range_value AND " . $end_time . " <= :field_data_field_event_date_range_field_event_date_range_value1)";
					$condition['field'] .= " OR (" . $start_time . " <= :field_data_field_event_date_range_field_event_date_range_value AND " . $end_time . " > :field_data_field_event_date_range_field_event_date_range_value1)";
					}
			}		
	}

}

}

Once you empty the caches hopefully it would achieve your goal.

Cheers

tobiberlin’s picture

I had no problem to use this module in that way: added two filters of that kind and was able to get the correct result - so far... I tested just with a few items

tobiberlin’s picture

Just to clearify... I had some issues as well but what solves it: put the two between filters into a filter group connected by "OR" (under "rearrange" on the administration part for filters on your views administration page) and connect this filter group with "AND" with all the other filters. In that way I was able to get a "from XY to Z" date filter listing all the events which appear to be valid in this time.

cmonnow’s picture

That's good. I never even installed this module since after reading the same issue in two posts I assumed it was not supported! I should test it in the future to see how the final query formula is arranged and if it's different to my method.

liquidcms’s picture

Version: 7.x-1.0 » 7.x-1.x-dev

i think my question is the same, but please tell me if it isn't.. i am trying to check for a range falling within a range.. basically the same picture as on the project page where i specify start and end and want to find any result in the 4 options shown (note: this isnt really the use that project maintainer intended for that picture).

the real world use of this is simple:
- i have a list of events with start/end dates
- i want to filter by semester
- semester does not have to fall cleanly on event dates: see picture from home page where semester start/end are the start/end dates shown.

if i add 2 of the view between filters with an OR function:
- type = Event
AND
- date between (semester start)
OR
- date between (semester end)

and i set these to match my semester, then it works for 3 of the 4 cases; but does not work for the 4th case where the event is completely contained within the semester.

liquidcms’s picture

maybe my issue is different than original poster's; but for my case as explained above, solution is:

between dates (term start)
OR
between dates (term end)
OR
start >= (term start)
and
end <= (term end)

which looks like this: http://screencast.com/t/B3C2pzqMW
and displays as: http://screencast.com/t/riWOvyTuo7

this covers the Views config part; but for my solution code is required to:
- hide all 4 filters
- replace with a single Term selector
- on submit look up start/end dates that go with Term selected and insert into the 4 filters i have hidden.

3man’s picture

As Tobiberlin already mentioned it works when you set the 2 between dates in OR group but when the "event” has the same start date and end date it does not work correctly. In my case some events are only one day.

Is it possible to show also the 1day events that are between the “between dates filter”

lubwn’s picture

As liquidcms mentioned in #8, this worked for me. Little tinkering with JS and hiding some fields with CSS and it works like a charm! Thanks a lot.

It would be handy if the module did it by default but well.. I guess it is too late to ask for it anyway, since D7 is ending its life soon.