I am creating a page managing concert dates. I constructed a view with an exposed filter for selecting a year to display all concerts taking place in that year. The exposed filter (appearing as a drop-down box) is configured to default to the current year ("now").

The issue is: the drop-down box always contains the current year +-3, so at the moment this is 2005, 2006, ..., 2008, ..., 2011. Regardless of whether there actually are any concerts in any of these years.

I'd like the drop-down box to contain only years where there are concerts in. For example, if I defined concert nodes (i.e. nodes with date cck-field) for 2006, 2008 and 2013, I'd like the drop-down box to only contain these three values. Is there any place where I could configure this?

At the moment, concerts taking place in years later than 2011 can't be displayed by the exposed filter because it contains only year values up to 2011.

Thanks in advance for any hints on this.

Comments

melvix’s picture

Here's how I did it.

In template.php:

function THEME_NAME_preprocess_date_views_filter_form(&$vars) {
  $form = $vars['form'];

  // query to select valid years where "event" is the content type we are filtering in the view
  // this query could be very different, depending on what date field you are actually querying
  $result = db_query("SELECT DISTINCT
          year(node_data_field_date.field_date_value) AS node_data_field_date_field_date_value
          FROM node node
          LEFT JOIN content_type_event node_data_field_date ON node.vid = node_data_field_date.vid
          WHERE (node.status <> 0) AND (node.type in ('event'))
          group by node_data_field_date_field_date_value
          order by node_data_field_date_field_date_value");

  $options = array();

  // "year" is the name of the year select in the form
  $options[] = array_shift($form['value']['year']['#options']);
  while ($year = db_fetch_object($result)) {
    $yval = $year->node_data_field_date_field_date_value;
    $options[$yval] = $yval;
  }
  // replace the default options with our new options array
  $form['value']['year']['#options'] = $options;

  // re-render the form
  $vars['description'] = drupal_render($form['description']) . drupal_render($form);

}

Replace THEME_NAME with your actual theme name.

If there's a better way to do this, I would love to know what it is.

origaman’s picture

Category: support » feature

Thank you very much melvix for posting this code. Works perfectly for me. I hadn't stepped into the php programming thing yet and it looks quite cryptic to me... But it works ;-)

So thank you, you made my day :-)!

Still I think this functionality should be integrated into views since the exposed filter is quite useless right now without this hack. So I'm keeping this thread open and changed the Category to "feature request": there should be at least an option for exposed filters to only expose values which are meaningful (or, non-empty).

origaman’s picture

Hm, I detected a bug in the code posted in comment #1. Normally, the exposed drop-down box contains a value "-Year" which, if selected, does something like "show all". It is still there and does the same thing with the code from comment #1 in template.php, but with a warning box appearing on top of the page content saying:

"An illegal choice has been detected. Please contact the site administrator."

How can I get rid of this warning? And, is there a possibility to rename the "-Year" option to something like "<all>"? This "-Year" value is completely meaningless for my site's visitors, as is the "Absolute value" title of the drop-down which I already got rid of by means of CSS.

I wouldn't go as far as saying exposed date filter usability "sucks", but I'm close ;-).

B00N’s picture

hi, you can do it like that:

--- snip ---
  $options = array('' => '-Jahr');
  // "year" is the name of the year select in the form
  while ($year = db_fetch_object($result)) {
    $yval = $year->yearvalue;
    $options[$yval] = $yval;
  }
  // replace the default options with our new options array
  $form['value']['year']['#options'] = $options;
--- snap ---
karens’s picture

Status: Active » Fixed

There were a couple other issues about this and I already commented on them. I committed some changes to the code so the filter will pick up the year range you put in the filter settings (it looks like '-5:+10'.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.