I am trying to setup a search for obituaries on a site. I am currently using views and can't get the hang of being able to give a viewer a choice of searching by date. I have tried the Date Range module and it doesn't seem to work at all or I don't have enough information about it to get it to work right.

Does anyone have a view they could provide so I can see how this should work and stop going in circles with it? I'm really frustrated and have gone to far to turn back now.

Thanks for any help ~ anything will be appreciated.

Comments

mooffie’s picture

I am currently using views and can't get the hang of being able to give a viewer a choice of searching by date.

There's a 'Node: Created Time' filter. Add it, select the 'Is Greater Than Or Equals' operator, and then turn it into an 'Exposed filter' (and check the 'Optional' box). Type 'From:' for the label.

Now,

If, and only if, the user types some date into this box, Views will give you only nodes created after (or on) this date. You may wish to put some default date there, such as '2001-01-01', so that users know what format is expected.

You do the same for the 'To:' filter. (But do this after you have finished with the 'From' filter because Views' GUI (not the engine itself) has some embarassing bugs with exposed filters.) Here as well you may wish to put a default date, such as '2009-01-01'.

(Later, for better usability, ask Views to print 'No obituaries were found matching the search criteria' when no nodes are found.)

adodd’s picture

Thank you moofie, you got me started on the right path. I have decided that the results I get from that are too inconsistent. It just doesn't seem to work right. I moved forward, instead adding my date as an exposed monthly dropdown list in my View. That works perfect with one exception. It shows Now (I would like it to automatically pull the right month name as the default value) and it lists the word ALL twice. I could live with that except only one of the word All works. I don't understand where I modify this.

This is how it looks in my view:

$view->name = 'obituaries';
$view->description = 'Obituaries';
$view->access = array (
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = 'Obituaries';
$view->page_header = 'Click here to search Archives older than January, 2007.';
$view->page_header_format = '3';
$view->page_footer = '';
$view->page_footer_format = '3';
$view->page_empty = 'There are no obituaries that match that criteria.
';
$view->page_empty_format = '3';
$view->page_type = 'node';
$view->url = 'new-obituaries';
$view->use_pager = TRUE;
$view->nodes_per_page = '30';
$view->sort = array (
array (
'tablename' => 'node_data_field_obit_date2',
'field' => 'field_obit_date2_value',
'sortorder' => 'ASC',
'options' => '',
),
);
$view->argument = array (
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'options' => 'nolink',
),
array (
'tablename' => 'node_data_field_obit_date2',
'field' => 'field_obit_date2_value',
'label' => '',
'handler' => 'content_views_field_handler_ungroup',
'defaultsort' => 'ASC',
'options' => 'default',
),
array (
'tablename' => 'node',
'field' => 'body',
'label' => '',
'handler' => 'views_handler_field_body',
),
);
$view->filter = array (
array (
'tablename' => 'term_node_8',
'field' => 'tid',
'operator' => 'AND',
'options' => '',
'value' => array (
0 => '62',
),
),
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node_data_field_obit_date2',
'field' => 'field_obit_date2_value_year',
'operator' => '=',
'options' => '',
'value' => 'now',
),
array (
'tablename' => 'node_data_field_obit_date2',
'field' => 'field_obit_date2_value_month',
'operator' => '=',
'options' => '%month',
'value' => 'now',
),
);
$view->exposed_filter = array (
array (
'tablename' => 'node_data_field_obit_date2',
'field' => 'field_obit_date2_value_month',
'label' => 'Select Month for 2007',
'optional' => '1',
'is_default' => '1',
'operator' => '1',
'single' => '0',
),
);
$view->requires = array(node_data_field_obit_date2, node, term_node_8);
$views[$view->name] = $view;

mooffie’s picture

I have decided that the results I get from that are too inconsistent. It just doesn't seem to work right.

We could investigate the problem. Maybe you entered the date in an American format?

I moved forward, instead adding my date as an exposed monthly dropdown list in my View. That works perfect with one exception.

Two.

it lists the word ALL twice.

Uncheck the 'Optional' checkbox.

It shows Now (I would like it to automatically pull the right month name as the default value)

In the 'Arguments' fieldset you'll find an 'Argument Handling Code' textarea. Paste there the following code.

// Note the number '3': it's because the month
// filter is your fourth.
$view->filter[3]['value'] = date('m');

Tip: If you have problems with dates in Views you can install the devel module and examine the SQL sent to MySQL.

adodd’s picture

Thank you so much for taking the time to look at this again. So far so good. That worked for the Argument! Unchecking the Option box did remove one 'All', unfortunately it removes the one that works.

I found the code in date_views.inc and removed '' => '<*ll>', Sure enough that removed the extra All.

$current = array('now' => t('now'));
$months = ($current + drupal_map_assoc(range(1, 12), 'map_month'));
$days = ($current + drupal_map_assoc(range(1, 31)));

I can't tell by looking at it why it wasn't working right. For now I am going to check the Option box (to use the All that was working) and move on. I would like to know if anyone thinks there will be new problems by making this change.

Thanks again for your help moofie, you have restored my faith that someone is listening. I know I am going to have many more questions this week because I'm supposed to go live with this site this week. Last time I checked my main Search mysteriously quit working with an "Internal Server Error", never had any problems with it and don't even know when it quit working. Also the site is extremely slow. Also, still trying to figure out a good way to display my archives. Lots to do!!

mooffie’s picture

Unchecking the Option box did remove one 'All', unfortunately it removes the one that works.

Yes, there seems to be a bug in the Date module. I'll try to investigate and report this later.

I would like to know if anyone thinks there will be new problems by making this change.

Only that you need to remember to make this change again if you update the Date module.

Also the site is extremely slow.

It's worth installing the 'devel' module for this as well.

BTW, after you install this module, and when you're at 'q=your-view-url-here', you'll find a new tab, 'Dev queries'. Clicking this tab will show you the SQL that Views eventually uses.

Search mysteriously quit working with an "Internal Server Error"

Check your server's "error log". I'm sure the procedure is described in the handbooks.

still trying to figure out a good way to display my archives

This shouldn't be hard. There are several ways to do this. The obvious one is to use a date argument (when an argument is missing you get a summary; see the handbooks --and I believe there's a receipt there for creating an archive).