I need to create an exposed filter to a view that should handle the content's post date and more specifically the year. So I need a jump list that should contain values like '2009', 2010', '2011' etc.

I read Exposed Post date filter on views and it's all very helpful but I cannot figure out how to automate the starting date so it can be the year of the oldest content posted an the ending one the year of the newest.

Any help?

  • 1
    I'm interested in this. I ended up just creating a custom block with some SQL queries. I also switched from exposed filters to contextual filters because it made for cleaner URLs – Johnathan Elmore May 2 '12 at 15:45

You'll need to use hook_form_alter in a custom module to set the options as the form is loaded.

Something like this:

function my_module_form_alter(&$form, &$form_state, $form_id) {

  switch ($form['#id']){
    case "my_form_id":

      //use db_select to get the oldest and newest years, 
      //loop through them to end up with an array like this one:        
      $years_array = array('2009', '2010', '2011');

      //set the options to your array of years
      $form_state['input'][my_form_input_name]['#options'] =  $years_array;
  • 2
    I'm thinking he wants to generate a list of available options and/or validate that the year is in the possible range. May just need to modify what you have above to make it work for the available options and not the selected option. – Johnathan Elmore May 2 '12 at 17:21
  • Re-reading the question you're right, I'll edit the example now – TCLopez May 3 '12 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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