Yes, i know...this is another issue about how to alter an exposed filter form and i've read a lot about it but that's my situation.
I have to provide the administrators of a website the ability to change the default value in an exposed filter so to show the content they like by default changing it by a simple setting in an administration page.
That's the case, i thought i could simply change the default value in the form and everything would have worked the way i wanted but after enabling the module i've created to do this the value in the form conseguently changed while the view remained exactly the same.
Not even the value in the select changed, while inspecting the form the #default_value is the right one, so i'm able to handle the form values but not to make them really change the views item being shown or the "selected" value inside the select form.
What to say about the view...it is a particular display of a view, it uses ajax, the form is autosubmitted using jquery and the exposed filter got a default value in the views ui.
I don't want to put code in this issue for now since everything is working about altering the form but i can't see any change in the view being rendered.
What am i doing wrong?
Thanks,
Da.
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedThe trouble is that $_GET forms are tricky, and #default_value doesn't actually always work.
So Views will actually put the default data in $form_state['input'][$identifier] which is a copy of $_GET normally. In order to modify this default value, you're going to have to modify that. BUT you're going to have to be careful not to overwrite any data that was actually sent, so you will need to see if either $_GET[$identifier'] is unset (at which point, modify the $form_state['input']) -- if you might be getting the input from another source than $_GET you can also check out...I think it's $view->exposed_input. I forget exactly but a dsm() should find it quickly.
Comment #2
planctus CreditAttribution: planctus commentedThank you so much merlin, everything is working now...
I'm impressed how you were able in such a short explanation to make everything clear to me...
I've just spent a little amount of time trying to find out which was my "source", if it was $_GET or anything else...at last it was the first one even if this is not something i could completely understand...
This opens an entire world of possibilities and i think it would be interesting to make this a plugin for views in order to give an "administrator" the ability to change the setting about a default value of an exposed filter without giving him the hard task to administer views, for example.
Thank you, again :-)
Da.
Comment #4
mrbangkok25 CreditAttribution: mrbangkok25 commentedHi,
I am trying to accomplish something similar.
I am trying to to set the default_value of a select list, with hook_form_alter.
I can see the value comes from $form_state['input'][$identifier], i am able to change that, but as mentioned in post #1 i apperantly overwrite the data being sent. Because whatever i chose in my select list, it always show the result of the default_value, and changes the selection of the select list back to it's default_value.
I also took al look at $view->exposed_input, but that is not set.
Am i missing something ? what am i doing wrong ?
Thanks,
Comment #5
dagmarPlease provide the code that you used.
Comment #6
myvo CreditAttribution: myvo commentedI have the same problem. Here's what I've been trying:
- My views type: block
- Use Ajax: YES
- Views version: I try to use other version 6.x-2.11
Comment #7
planctus CreditAttribution: planctus commentedI'm sorry i didn't post my solution until now, i'll give an example of what i did, you'll see it is very simple:
Basically, you check that the form values are not already submitted and then put your desired value for a field.
In my situation i had a form to store that default value, letting the admins choose it so i could retrieve it using variable_get().
I hope this will help users to get through this.
Da.
Comment #8
esmerel CreditAttribution: esmerel commentedIs this something that should get documented? Patched?
Comment #9
nicksanta CreditAttribution: nicksanta commentedI had this problem too. @merlinofchaos's solution in #1 didn't solve the problem for me however, as I simply wanted a default value sitting in the form field without the default value passed into the query.
I came up with this solution which isn't particularly clean, but got the job done.
Comment #10
iamjon CreditAttribution: iamjon commentedClosing from lack of activity. Please feel free to reopen.
Comment #11
WildKitten CreditAttribution: WildKitten commentedJust to give some explanation.
in #7 :
print $form['#id'];
in same functionin #9
print $form_state['view']->current_display;
in yourmodule_hook_alter() function.Comment #12
Amir Simantov CreditAttribution: Amir Simantov commentedThanks for this thread, I think I am in the right direction. However,I have trired #7 and I might need some help.
Here is my code:
The Filter identifier (under 'More' fieldset) in my case is 'year' and is what I understand to be the 'exposed_filter_name', or isn't it? However, trying to print $_GET['year'] gives me an error of undefined index.
What do I do wrong?
Thanks!
Comment #13
planctus CreditAttribution: planctus commentedYou might simply try to check if the filter was already used doing this:
if(!isset($_GET['year']))
See you,
Da.
Comment #14
Amir Simantov CreditAttribution: Amir Simantov commented@planctus
This does not help me because I need to make filtering, not to check whether filtering was made...
Comment #15
JSCSJSCS CreditAttribution: JSCSJSCS commented#7 does not work for me either. I think it is because I am exposing the location distance/proximity field which really exposes three filters (postal code, distance, units of distance) under one filter identifier (distance). Just checking if it is blank doe snot work because the miles has a default value (10) and so does the units (miles). Leaving the zip code blank still gives a distance output, but one with no zip code.
I do;t know how to modify #7 for my use case.
My URL string after input:
It really gets messy when there is a pager!
Since I am showing a gmap of locations, I want to stop someone from entering and empty zip code exposed filter, as that map takes forever to load.
Comment #16
Frippuz CreditAttribution: Frippuz commentedA comment on this.
I tried to add a "Please choose" alternative to a views exposed filter. Among other things I hade problems adding my new option to the top. I also had problems setting the default option. In the end I ended up with the following:
It's strange that I need to set "#value" instead of "#default_value".
Comment #17
milehighlife CreditAttribution: milehighlife commentedJSCSJSCS,
I'm curious if you were able to solve your issue with the 'blank zip code field' and what you did? I have a related problem: if a zip code is not given, I'm wanting to unset the distance field.
Thanks!
Comment #18
purabdk CreditAttribution: purabdk commentedForm alter will does not override the default value of an exposed filter.
you need to use the view hooks for that. I used following code in my module. which is working fine.
Comment #19
merlinofchaos CreditAttribution: merlinofchaos commentedform alter can override the default value of an exposed filter, but you need to check and potentially set $form_state['input'] instead. This is because GET forms are automatically considered to be submitted whether or not a button was pressed because there isn't a POST event to check.
Comment #20
xeeshangulzar CreditAttribution: xeeshangulzar commented#7 works for me. Great Job!
Comment #21
helenc CreditAttribution: helenc commented#7 works for me also, with the exception of using
!isset
rather thanempty
, e.g.The reason being that 'empty' will return true for a value of '0', and my select box has 0 for one of the values.
Comment #22
sylvaticus CreditAttribution: sylvaticus commentedI can't get #7 to work, maybe because in my case the exposed field is a node reference field.
Using dpm($form) and dpm($form_state) I can see that the values are actually changing, and so they do if I manually select the desidered option, but I can't set the default option on the desired value.
Any hint ?
Comment #23
dagomar CreditAttribution: dagomar commented@sylvaticus
I had the same problem, however I think it was due to some browser caching, so I suggest to try the result in an incognito window. Then it worked for me!
Comment #24
mudjunky CreditAttribution: mudjunky commentedI could not get #7 to work in 6.x-3.2. I used this instead:
Comment #25
niccottrell CreditAttribution: niccottrell at Transmachina commentedIn Drupal 8.x something like this should work:
Comment #26
jbd44 CreditAttribution: jbd44 commentedTo set a default value: