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
Comment #1
netbear CreditAttribution: netbear commentedTried also do in such manner with form_id = 'views_exposed_form'
Also doesn't work.
Comment #2
merlinofchaos CreditAttribution: merlinofchaos commentedYou 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.
Comment #3
netbear CreditAttribution: netbear commentedThanks, merlinofchaos.
I'll gave it a try again.
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.
Comment #4
netbear CreditAttribution: netbear commentedOops, 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:
Comment #5
netbear CreditAttribution: netbear commentedStill 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."
Comment #6
netbear CreditAttribution: netbear commentedOk, the second problem is gone when I changed my options list this way:
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?
Comment #7
merlinofchaos CreditAttribution: merlinofchaos commentedSorry, I'm a dork. In D6 it's $form_state['view'].
Comment #8
netbear CreditAttribution: netbear commentedThanks one more time, merlinofchaos, that worked fine, I've checked
if($form_state['view']->name == 'lessons_list') {
}
And everything is Ok!
Comment #10
jfama CreditAttribution: jfama commentedTwo years later, this thread saves my hide. Thanks a bunch guys!
Comment #11
sydneyshan CreditAttribution: sydneyshan commentedWhile 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:
Comment #12
TechNikh CreditAttribution: TechNikh commentedThanks for this post. It saved me a bunch of time.
Comment #13
taggartj CreditAttribution: taggartj commentedhttp://drupal.org/node/1097996#comment-5463368
check that out
Comment #14
谢艳 CreditAttribution: 谢艳 commentedComment #15
nehapandya55 CreditAttribution: nehapandya55 commentedThanks 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.
Comment #16
louisnagtegaal CreditAttribution: louisnagtegaal commentedBTW: 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'
Comment #17
sri_techie CreditAttribution: sri_techie commentedI'm unsetting view's exposed filter field 'membership_type' multi select field options.
Hope it helps some one in future :)