0

I have a view with exposed filters in Drupal 7 and I need to set the default value of a filter to current year. I also need to unset/remove the - Any - option from the select list.

3

You can use hook_form_alter() to alter the any drupal form. example :

function yourmodule_name_form_alter(&$form,&$form_state,$form_id){
  if($form_id == 'views_exposed_form'){
  $form['field_width_value']['#default_value'] = 'whatever you want';
  unset($form['field_width_value']['#options']['All']);

 } 
}
  • I am getting: [field_year_value] => Array ( [#type] => select [#default_value] => All [#options] => Array ( [All] => - Any - [1] => 2011 [2] => 2013 [3] => 2015 ) [#bef_description] => 2015 [#description] => 2015 ) but this is not changing my default value – jsh Apr 15 '15 at 7:28
  • $form['field_year_value']['#default_value'] = '2015'; I am using this .. but not working for me – jsh Apr 15 '15 at 7:30
  • you should use key of the value([3] => 2015). example $form['field_year_value']['#default_value'] = 3; – Ravi Barnwal Apr 15 '15 at 7:35
  • its working but only one time.. when i cleared cache.. If I select other year and then refresh page..then doesn't show default value. – jsh Apr 15 '15 at 7:43
  • Have you set remember option in views filter option? Because my side this is working fine – Ravi Barnwal Apr 15 '15 at 8:07
2

My case scenario for a taxonomy field and filtered results, not just default value:

if ($form_id == 'views_exposed_form') {
  $account = user_load($user->uid);
  $country = taxonomy_term_load($account->field_country[LANGUAGE_NONE][0]['tid']);
  $form['country']['#options'] = array($country->tid => $country->name);
  $form['country']['#default_value'] = $country->tid;
  $form_state['input']['country'] = $country->tid;
}
  • the problem is that when you change the value and submit the form it sets it back to the default value – Beniamin Szabo Nov 19 at 10:42
1

where are you going to set default value of current year? If you want to remove -any- from options, make it required. or use Better Expose Filter to override the -any-.

I'm not sure if this post is your duplicate but I'm going to put my answer here also.

An expose filter but by default is the current year.

This is what you're going to do.

  • Add your date field to your filter criterion
  • Date selection form element: SELECT
  • Filter granularity: YEAR
  • Expose it
  • Operator: Is equal to
  • Choose "Enter Relative Date"
  • Type this: now

First Part:

enter image description here

Second Setting: enter image description here

Expose Filter with Default Value

Expose Filter with default

Is this what you want?

  • I want to remove -any- from options. And my filter like this:year[2014][2015][2016] something like this. I need to set by defualt value current year . – jsh Apr 15 '15 at 7:05
  • I edited my answer, please have a look – Danz Apr 16 '15 at 1:09
0

Having also this need I just found this module: Views Advanced Labels and successfully used it, replacing the - Any - by a custom simple text for 2 exposed filters in the same view.

It says that it works with tokens. So I guess using [date:custom:Y] or maybe creating a date format could be an option.

Some more resources here: Auto update to the current year in the copyright footer (© 2010-2013)

-1

This is my solution. Working for Drupal7

function **THEME_NAME**_form_alter(&$form,&$form_state,$form_id){
    if($form_id == 'views_exposed_form'){
      $form['**FIELD_ID**']['#options']['All'] = '**YOUR TEXT**';
    }
}
-1
function yourmodule_name_form_alter(&$form,&$form_state,$form_id){
  if($form_id == 'views_exposed_form'){
  $form['field_width_value']['#default_value'] = 'whatever you want';
  unset($form['field_width_value']['#options']['All']);
 } 
}

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.