In my case exposed filter form contains filter of content by average vote results, I called this filter "average" and choosed "more or equal to" in the views user interface for this filter. And in my exposed filter form appeared textfield for user to write number. But for voting I use fivestar module and I would like to have a dropdown with (*, **, ***, ****, *****) options in my filter instead of textfield.
So I tried to make little module with form_alter hook like that:

function mytools_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'views_exposed_form__lessons_list__default') {
    //unset($form['average']);

    $form['average'] = array(
      '#type' => 'select',
      '#options' => array(0 => t('<any>'), 1 => '*', 2 => '**', 3 => '***', 4 => '****', 5 => '*****'),
      '#default_value' => 0,
      '#title' => t('The average vote is more than',
    );

  }
}

My view has name "lessons_list". And display is "default".
But no changes in exposed form happend, though I resubmitted view to be sure.
So the question is how to alter exposed filter form and what I do wrong?

Comments

netbear’s picture

Tried also do in such manner with form_id = 'views_exposed_form'

function mytools_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'views_exposed_form') {
    //unset($form['average']);

    $form['average'] = array(
      '#type' => 'select',
      '#options' => array(0 => t('<any>'), 1 => '*', 2 => '**', 3 => '***', 4 => '****', 5 => '*****'),
      '#default_value' => 0,
      '#title' => t('The average vote is more than'),
    );

  }
}

Also doesn't work.

merlinofchaos’s picture

Status: Active » Fixed

You should be able to examine $form['#view'] to see which view is being displayed, and the form id should aways be 'views_exposed_form'. Beyond that, all other form alter tricks should be normal.

netbear’s picture

Thanks, merlinofchaos.
I'll gave it a try again.

function mytools_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'views_exposed_form') {
    //unset($form['average']);

    $form['average'] = array(
      '#type' => 'select',
      '#options' => array(0 => t('<any>'), 1 => '*', 2 => '**', 3 => '***', 4 => '****', 5 => '*****'),
      '#default_value' => 0,
      '#title' => t('The average vote is more than'),
    );

  }
}

And it worked fine.(My filter was named averag instead of average).
But in the form I've not found $form['#view']. I created template exposed_filter_form.tpl.php in my theme and printed form array: print_r($form). It gave me Long-long array but there was not $form['#view']. Also checked with print_r($form['#view']) - nothing returned. So I couldn't alter exposed filter form only for my view. Also while testing I've discovered that the filter provided by votingapi module doesn't work at all, the content is not filtered with or even without my form alter.

netbear’s picture

Oops, everything is working, my fault, I've forgot to adjust my relationship correctly (value type: percent ; vote tag: vote ; aggregation function: average) and one more detail - filter waits for value in percents like 20, 40, 60, 80, 100 but not 1,2,3,4,5, so I needed to make little change in my module:

function mytools_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'views_exposed_form') {
    //unset($form['average']);

    $form['average'] = array(
      '#type' => 'select',
      '#options' => array(0 => t('<any>'), 20 => '*', 40 => '**', 60 => '***', 80 => '****', 100 => '*****'),
      '#default_value' => 0,
      '#title' => t('The average vote is more than'),
    );

  }
}
netbear’s picture

Still have problems:
1) how to deal only for necessary view exposed_filter_form, excluding views where it is not needed?
2) when I go to the view page with my filter not submitted there is error message "An illegal choice has been detected. Please contact the site administrator."

netbear’s picture

Ok, the second problem is gone when I changed my options list this way:

     '#options' => array('' => t('<any>'), 20 => '*', 40 => '**', 60 => '***', 80 => '****', 100 => '*****'),

Please help me to exclude exposed filter forms of other views from my form_alter hook action.
How to define there(in form alter hook) what view the form belongs to?

merlinofchaos’s picture

Sorry, I'm a dork. In D6 it's $form_state['view'].

netbear’s picture

Thanks one more time, merlinofchaos, that worked fine, I've checked
if($form_state['view']->name == 'lessons_list') {

}
And everything is Ok!

Status: Fixed » Closed (fixed)

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

jfama’s picture

Two years later, this thread saves my hide. Thanks a bunch guys!

sydneyshan’s picture

While we're reviving old threads... (and thanks to 1st hit on Google for this issue) I thought I'd ad my method of targeting a specific views filter form (using the #theme key:

function hdcdine_form_views_exposed_form_alter(&$form, &$form_state){
	if($form['#theme'][0] == 'views_exposed_form__Dining_Venues__page_1'){
		$form['ct']['#options']['All'] = 'All Cuisine Types';
	}
}

TechNikh’s picture

Thanks for this post. It saved me a bunch of time.

taggartj’s picture

谢艳’s picture

function mymodule_form_alter(&$form, &$form_state, $form_id){
    if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'myviewname' && $form['#id'] == 'views-exposed-form-myviewname-page-1'){
    $form['field_province_tid'] =array(
       '#type' => 'select',
       '#options' => $province_options,  
       '#default_value' =>$default_province,
       '#ajax' => array(
        'callback' => 'ajax_parovince_select_callback',
        'wrapper' => 'edit-field-city-tid',
        'method' => 'replace',
        'effect' => 'fade',
        ),
      );
  }
}
nehapandya55’s picture

Issue summary: View changes

Thanks netbear
In drupal 8 when i added filter using hook_form_alter i also got error as "2) when I go to the view page with my filter not submitted there is error message "An illegal choice has been detected. Please contact the site administrator."
#6 worked for me.

louisnagtegaal’s picture

BTW: in Drupal 8 you can get the view with:

$form_state->getStorage()['view'];

You will need the view if you want to alter a specific exposed filter form because $form_id will always be 'views_exposed_form'

sri_techie’s picture

I'm unsetting view's exposed filter field 'membership_type' multi select field options.
Hope it helps some one in future :)

/**
 * Implements hook_form_alter().
 */
function myModule_form_alter(&$form, &$form_state, $form_id) {
		
  if($form['#id']== 'views-exposed-form-members-directory-page') {
     // dsm($form['membership_type']['#options']);
     foreach ($form['membership_type']['#options'] as $key => $value) {
                  
        // Check if the key is 6,7,8 and unset. i.e., unset Full Monthly, Retired Monthly, Trainee Monthly
        if($key == '6' || $key == '7' || $key == '8' ) {
	      unset($form['membership_type']['#options'][$key]);
	  }
      }
			
   }

}