Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is there a way to change the order of exposed filter fields by hook_form_alter'ing their weights? I'm trying to add a few new form elements with hook_form_alter and can't get them to show up anywhere except the end of the for.
The other alternative I come up with is perhaps a plugin that just adds blank form elements to filters, is such a thing feasible?
Comments
Comment #1
dawehnerit's fapi so it has to work :) You probably doing something wrong here.
Comment #2
dabblela CreditAttribution: dabblela commentedI would think so too but I cannot seem to make it work. If I make a view with just NID and Node Title exposed filters, set NID to come before Title in the UI but make $form['title']['#weight'] higher than the nid weight in my alter, there is no change.
Comment #3
dawehnerThe problem is that there is some theming on exposed form. You would have to override it, sadly.
Comment #4
esmerel CreditAttribution: esmerel commentedComment #6
LGLC CreditAttribution: LGLC commentedHas anyone got an example of how to do such theming? I'm really struggling trying to get new form elements to the the top of exposed filter forms.
Thanks.
Comment #7
dabblela CreditAttribution: dabblela commentedYes, all of it is handled in views-exposed-form.tpl.php, which you can override in your theme to print out your fields manually. IIRC, the extra fields are gonna be in the $button variable in that template, so you might want to do some preprocessing in template.php
Comment #8
LGLC CreditAttribution: LGLC commentedThanks for your help, manatwo. I finally managed to do it after reading your post. Thank you.
Comment #9
kgthompson CreditAttribution: kgthompson commentedI was able to alter the order without overriding the template. I am using Views 3, but I think this will work in Views 2 as well.
Alter the order of the filters in $form['#info'] in your form_alter and this should alter the order the widgets print out in the template without having to do any custom templating.
Comment #10
maciej.zgadzaj CreditAttribution: maciej.zgadzaj commentedA small update on this, if someone faces similar problem in the future, as I just did myself, trying to place my new exposed form item in-between "widgets" generated automatically by the view - the solution is to turn that new form item into a widget, and then re-order widget array in the
$form['#info']
.In my case I was adding new
community
form item into Views exposed form, and then wanted it to be displayed as a second element in the form, right afterfilter-title
generated by the view:Obviously the code responsible for re-ordering
$form['#info']
will look different depending on where the new form item should be displayed.Comment #11
MixologicIm re-opening this as Im hoping somebody with deeper views experience can chime in on the best way to handle this.
I too needed to shim in a #markup bit in between various exposed filters, and I did exactly what maciej.zgadzaj did above. All the while I was wondering why a form alter, or even a form #afterbuild seems to require such jostling of form['#info'] just to reorder the widgets. Is there a better way to accomplish this?
Comment #12
dotist CreditAttribution: dotist commentedthe solution from maciej.zgadzaj in #10 worked great for me - thanks!
Comment #13
lpeabody CreditAttribution: lpeabody commented#10 worked for me as well. Totally weird...
Comment #14
blacklabel_tom CreditAttribution: blacklabel_tom commented#10 worked for me too.
Any plans to get the form to respect #weight?
Cheers
Tom
Comment #15
michaellenahan CreditAttribution: michaellenahan commentedThank you very much maciej.zgadzaj for #10!
Here is some code for putting an element into an exposed form at a certain position.
In this case, $insert_index = 2 means that the new element will go after the second element.
Comment #16
leex CreditAttribution: leex commentedYou can use normal weights if you're willing to ignore templating, which for me was totally acceptable. I set weights on exposed filters using form_alter:
Then to ignore templating, just put an empty theme hook in template.php:
Yes this is not a very ideal way of doing it as it removes templating from all views exposed filter forms, but it was much quicker for me than doing the ordering properly.
Comment #17
profak CreditAttribution: profak commentedMaciej.zgadzaj's solution is great!
Thanks!
#weight still not supported. It looks fine because of way widgets render and work.
Comment #18
drupalshrek CreditAttribution: drupalshrek commentedThank you very much michaellenahan for your code in #15 as this works great!
Comment #19
drupalshrek CreditAttribution: drupalshrek commentedMoving the code from michaellenahan into a function:
Example of call to the function to add 2 fields.
Comment #20
DrCord CreditAttribution: DrCord commentedThank you drupalshrek, maciej.zgadzaj and michaellenahan!!
Comment #21
jedihe CreditAttribution: jedihe commented#15 worked great for me! thanks!
Comment #22
bas.bakker CreditAttribution: bas.bakker commented#15 worked like a charme
Comment #23
sebi CreditAttribution: sebi as a volunteer commentedIf you dont care about theming, you can simply set the parent element
['#theme']
to an empty string to reset formatting and handle weights.In my case...
Comment #24
jfraz CreditAttribution: jfraz commented#15 FTW. Thanks michaellenahan!
Comment #25
ropic CreditAttribution: ropic as a volunteer commented#23 works too, great !
Comment #26
konst_8 CreditAttribution: konst_8 commentedYou can use drupal element_sort function, applied to $form['#info'] array:
Comment #27
riddhi.addweb CreditAttribution: riddhi.addweb at AddWeb Solution Pvt. Ltd. commentedComment #28
Lissy CreditAttribution: Lissy commentedkonst_8, You made my day. ;-)
#26 works perfekt for me.
Comment #29
Meera.b CreditAttribution: Meera.b commentedComment #30
Meera.b CreditAttribution: Meera.b commented#10 worked perfectly.
Comment #31
Collins405 CreditAttribution: Collins405 commentedTried everything, 26 was the only one that worked for me. Thanks!
Comment #32
sushantpaste#10 Works fine !
Thank you.
Comment #33
sergey-shulipa CreditAttribution: sergey-shulipa as a volunteer commentedThanks to all! But any solution as-is not worked for me properly.
Problem was in all widgets, corresponding to $form['#info'] array elements, which was below new widget in $form['#info'] array. So, these widgets appeared inside new widget.
My solution:
I've combined #10 and #26, but added operator to new $form['#info'] element:
Comment #34
MustangGB CreditAttribution: MustangGB commentedSeveral people reporting the same thing, seems more like a bug at this point.