Summary:
To summarize what I've written below, when I click the "Apply" filters button, I incorrectly get redirected to the homepage.
Facts:
View:
I have a view named "browse", in which I have a simple defaults display with these attributes...
-Fields: a few basic fields like Node:Title (doesn't matter which fields... this bug persists, regardless)
-Filters: Content:Region (exposed; "Region" is a content taxonomy field)
Node Content Type:
I have a content type called "myregion", and in node-myregion.tpl.php I put this code:
print views_embed_view('browse','default');
Specific Node:
I created a node (title: "Browse1"), which, due to my autopath settings, resides at mysitename.com/myregion/browse1
Problem:
When I select a region from the exposed filter select list and then click "Apply", I'm redirected to my homepage (in this case, to mysitename.com/?region=38) rather than back to the same page (to mysitename.com/myregion/browse1?region=38).
When I open Firebug and click on the "Apply" button, I see in the DOM section that the form action (DOM > Form > action) is set to just mysitename.com and when I change this to mysitename.com/myregion/browse1, the page redirects to itself as it should. I'm not sure if this is a Views bug, or if it has something to do with FAPI or something else in Drupal core (I'm no expert in any of these things).
Notes:
-When I preview my view in the Views UI, the "Apply" button works as it should.
-As a temporary fix, I am using AJAX for this view, which doesn't result in this redirection problem. Also, if I create a "page" display and view that page, it correctly redirects to itself upon clicking the "Apply" button. Neither of these two workarounds are desirable for me long-term, though.
Comments
Comment #1
Chris Einkauf commentedI think I may have stumbled upon a partial answer to my own question. It appears that the function theme_form() has something to do with it (so I've changed this to a support request rather than a bug report because it looks like Views might not be the culprit, but I'm still not entirely sure).
Here is the default theme_form() code:
And here is the new code after I modified it and stuck it into my template.php:
Notes:
-My theme name is "mabtheme"
-The if statement basically looks to see if the ID of the form is "views-exposed-form-browse-default" (an exposed form for the "default" display of my "browse" view). If it is, it uses /browse as the action. If it's not, it uses the regular old code to determine what the action should be.
I still wonder, though, if it's a Views bug or a core bug (or maybe not even a bug at all and I just simply don't understand what's happening here). For example, is it Views' fault that the already-existing $element['#action'] is set to "/"?
Comment #2
merlinofchaos commentedThis isn't exactly a bug, it's a misunderstanding.
Views assumes that exposed filters might appear on a page other than where they intend to appear. Therefore they lead back to the 'link display'. If there isn't a page display on the view, then they link to the home page.
When embedding the view, you need to set $view->override_path in order to get these filters to go somewhere else. Which means you can't use views_embed_view(), but instead need to do a $view = views_get_view(), $view->override_path = $_GET['q'] and $output = $view->preview().
Comment #3
Chris Einkauf commentedThank you merlin! It's working just as I wanted now.
In case anyone else ever reads this and wants to know the exact code I put in, here it is (I put this in my custom node-myregion.tpl.php):
Comment #4
JirkaRybka commentedFor anyone interested: I tried to open this "bug" at #156130: Exposed filters: URL processing on embeds, because it's really anoying malfunction in certain situations (having an edge case as default, IMO). Unfortunately, the module author thinks otherwise, so no help here... Views is too complex module to be sure about anything, so I just believe Merlin is right.
Comment #5
harkaman commentedHi eink21:
Does the fix work for the ajax support as well? I had the same problem of page being redirected to the homepage after clicking on the "apply" button of the exposed filter for an embedded view. Merlin's solution fixed the redirection problem. However, the ajax support is still broken. Thanks.
Comment #6
Chris Einkauf commentedHarkaman: I believe the ajax all worked for me for the scenario I explained above. Is all ajax functionality broken for you? (I.e. do things like the pager still work?)
The reason I say "I believe" it worked for me is because I've since made my views/filters a bit more complicated. Long story short, I have one view that houses just the exposed filters in a block and several other views that use those filters to filter their data. Although the view with the filters doesn't use ajax (so as to show the filter variables in the URL, which the other views can use), all of the other views on that page do, in fact, use ajax successfull (for paging).
Comment #7
harkaman commentedeink21:
Ajax for both the pagination and exposed filter fail for the embedded view. In your case, are the views that use ajax successfully for pagination embedded views (using the code given above)? Just wanted to verify if ajax actually works for embedded views.
My goal is to use to make the taxonomy terms from an exposed filter clickable and have ajax return the data (with pagination) and at the sametime show the set of returned data on a gmap. For some reason, I am stuck on the initial phases. I will post as I progress, however.
Many thanks!
Comment #8
Chris Einkauf commentedWhen you say "embedded views", I think you might be referring to one of these 3 things:
1. using the views_embed_view() function
2. using the Views Embed module to attach an 'embed' views display to a content type via the Views UI
3. using an 'attachment' display to "embed" the view into another view (e.g. attaching it to the defaults display)
The method that ended up working for me, though, was none of these things. As for #1, the code that I put in comment 3 above doesn't require any use of the views_embed_view() function. As for #2 and #3, I didn't use either the 'attachment' or 'embed' views displays - I just used the defaults display.
Does this help you out at all? Let me know if I misunderstood you.
Comment #9
harkaman commentedHi Chris:
I did mean using the code in your comment # 3 when I was referring to embedded views. Does ajax support work for the exposed filter and pagination for views "embedded" using the code in comment # 3?
Also, I am trying to accomplish something like http://drupal.org/node/473510. Means, I will have to take a thorough look at the views module.
Thanks.
Comment #10
Chris Einkauf commentedWell I can definitely say that ajax for pagination works with that code. My filters are contained in a separate view (on the same page), which doesn't use ajax so i can't speak for that. Btw, if you're having trouble with ajax working, perhaps clearing your cache might do the trick. That solved a few weird views things for me, at least.
The page about Theming views exposed forms from a select box into a list of links... that you linked to is interesting. I wouldn't mind having that functionality on my own site, so I'll take a look at it over the next day or two and post my solution once I come up with it.
Comment #11
Chris Einkauf commentedSee my response over at the page you mentioned. It's not the exact code you'd need for ajax to work with the filters, but it's a good starting point.
Comment #12
harkaman commentedThank you so much for the code!
Comment #13
Chris Einkauf commentedYou're welcome. I still have a few decisions to make in terms of how I want to handle various filters on my site, but if I end up using this approach I'm sure I'll make some improvements to that code. If so, I'll try to remember to post a new-and-improved version.
Comment #15
alextronic commentedYes, it works, but, how can we get rid of all the variables in the URL? Now it's long and dirty. Used to be clean and pretty.
thanks to you and merlin for the code.
Comment #16
alextronic commented'Active' because it's important to know about how to obtain a clean URL with the provided code.
Comment #17
dawehnerDid you tryed out
??
Perhaps try out
Comment #18
alextronic commentedThanks for your answer, dereine.
Unfortunately, both methods work fine but none of them gets rid of the long URL.
We'll have to find something different.
A.
Comment #19
dawehnerWhat is your definition of "long url".
You didn't described yet, what this is.
Comment #20
alextronic commentedYes, sorry.
This is the URL that I get when clicking on "Apply" button:
http://www.example.com/browse-groups?title=name&province=Arizona&field_field1_value_many_to_one=All&field_interest_value_many_to_one=Family+or+Friends
I have enabled Clean URLs so I don't know why using this code results is this "long URL" in the browser.
browse-groupis the alias path to the node. The rest is filters selected in the View.Comment #21
JirkaRybka commentedThat's because all the exposed filters selections are in the URL (using GET form method). That's normal, and by design. I believe that the point here is, that you're able to bookmark (or link to) such a page, which you can't with a "normal" (POST method) form where the values won't be in URL.
Clean URLs only get rid of the "?q=", which would be in front of the page alias otherwise.
Comment #22
alextronic commentedYou're absolutely right about Clean URLs, I don't know what I was thinking.
But, all in all, this doesn't satisfy me very much... I mean, I'm really happy with what I have accomplished, but now there is a page in my site with this long and not-very-discreet URL.
I guess it will be marked as "by design" and we'll be 'content'.
Thanks.
Comment #23
Chris Einkauf commentedAs the person who wrote the code that you're referring to, I have to admit that I'm not a big fan of it either. The code itself is kind of long and ugly, plus, like you (alextronic) have alluded to, the code requires the GET method along with "long" urls (i.e. loaded with all of the variables).
I'm really not much of a programmer, myself - I just started learning PHP this summer and still think I have a long way to go. This leads me to believe that there MUST be some way to turn the select box options into links that somehow can use the POST method (and thus work with AJAX and show "pretty" / "short" URLs). I'm saying this because if it's possible with an apply button, then it's got to be possible with either some kind of special link, or via the combination of a link and a hidden form/virtual redirect/etc. Unfortunately, I just haven't figured out the easiest way to do it yet.
One possible hack would be to use the code I provided (i.e. still use the GET method), but then somehow hide that long URL from the user. Check out this comment for some sample implementations of this. Again, it's really just a hack because it still doesn't use the POST method which is what you'd need to get it working with AJAX, so improvements to my code would still be beneficial.
FYI... I, myself, decided not to even use the code I provided on the other page. I decided to install Apache Solr on my server, coupled with the Apache Solr Integration drupal module, which automatically turns things like taxonomy terms into clickable links.
Comment #24
merlinofchaos commentedSorry, this is By Design with Views, as mentioned earlier. Those URLs can be shortened somewhat by making nicer identifiers in the filter settings, though.
Comment #25
rickh commentedI apologise if this is a silly question but I obviously do not have a "myregion", so can anyone tell me what I should replace in "node-myregion.tpl.php", as in which varaibles should I use. Also where should the file be placed. Any help would be appreciated. Thanks guys, and sorry for being a dummy.
Comment #26
asiby commentedThis is very strange. the syntax of views_get_views() is ... (http://drupalcontrib.org/api/function/views_get_view/6)
Parameters
$name The name of the view.
$reset If TRUE, reset this entry in the load cache.
Return value
$view A reference to the $view object. Use $reset if you're sure you want a fresh one.
If the 'default' parameter is referring to the default display in the views, I think you should be doing the following instead ...
Cheers
Maestro
Comment #27
asiby commentedSorry guys, I have accidentally changed the title of this ticket. I am putting it back.
Comment #28
jisuo commentedI added this code to my tpl file
but it still redirects to the front page? The $_GET['q'] shows a node/123 url when debug printing, while my page is aliased similar to stay/cabins. Shouldn't be a problem right?
Ok, stupid thing: viewfield module wasn't updated to latest dev version. Works now. I just have to fix the node/123 to my alias url.
Comment #29
fourmi4x commentedHi,
Just to say that I tried all the solutions described here and none of them worked for me. I simply wanted to ambed an Ajax view with exposed filters.
After a lot of struggling, I finally discovered that the embed code had to be put in the node.tpl.php template instead of the page.tpl.php... and now it works perfectly, no need to set arguments, override the path, create a page display or anything...!!
Hope this will help some other newbies like me...
EDIT: I finally found out that my issue was more related to ajax than to the override-path problem, as described here: http://drupal.org/node/386388. This explains why an ajax view with exposed filters/pager works on a node.tpl but not on a page.tpl. Embedding a view can sometimes be really tricky!
Comment #30
henrijs.seso commentedOne more thing for people using "Content pane" display in views. You need to set "Use Panel path: Yes" in order to fix this issue.
Comment #31
ikeigenwijs commentedLooked ages for this. thx
Comment #32
goron commentedExtremely useful fix. This was very hard to find.
My problem was that AJAX was failing for exposed filters in views blocks on EI8. I simply added the following to a custom module to fix this:
Comment #33
goron commentedWouldn't it make sense for this to be the default behavior, at least for views blocks that have AJAX turned on? Or even for any block view that doesn't have a page display to link back to? In this case the exposed filter can degrade gracefully if JS is not present if the override path is automatically set to the current page rather than the home page.
Does anyone else think this is worth a feature request?
Comment #34
beto_beto commentedhello i have the same issue here
i am using expose view to search for Letter A as example
it redirect me to my default page with this URL >> example.com/?keys=&Letter_En=95
how should i fix this issue i am new D6 here
Silly me :S
i found the solution here :P
i am using "Content pane" display in views. You need to set "Use Panel path: Yes" in order to fix this issue.
Comment #35
timmetj commentedThe solution by adding the override path doesn't help for me.
I'm using a AJAX submit, so when i put the override path, it works when i only use the "enter" button, or only use the submit button. (in IE8)
When i use the "enter" the page refreshes, what i don't want it to do, but shows the good result. When pressing the submit button, ajax works normal.
But when first clicking the submit button, and then enter a new keywork and press "enter" the page redirects again to its original page.
This only happens in IE8. The "enter" command must use the same function as the submit button, but looks like something is wrong there.
I am on latest views and Ctools using Drupal 7
Comment #36
mazdakaps commentedi maybe have similar problem and i just noticed that. I didnt have that problem for sure some days ago. For some reason after i hit submit with ajax in a content pane in views on an exposed filter the reset button dont link to front page anymore but it links on the first page i have in the same view. At first i was using a block but today i saw that the link on reset button wasnt linked to front page like it was on past days so i read this topic and i change it to content pane and enabled Use panel path to yes. The reset button was at the front page as i wanted but after i hit submit the link changes again. I disabled ajax on the content pane and it seems that is working. But i need to use it with ajax and i cant understand how this happened all of a sudden.
Thank you
Comment #37
mazdakaps commentedwell i fixed it by seperating page views and block or content pane views in two different views. Sorry if this wasnt realated with the topic
Comment #38
aendra commentedRe: #30 -- Thanks a tonne, mansspams! That totally solved my issue with Views Content Panes. Would've been looking for that forever otherwise!
Comment #39
stevieb commented#32 worked sweetly for me - thanks
Comment #40
mpotter commentedgoron: Thanks very much! #32 worked for me with my ajax'd content panes. Just #30 didn't help.
Comment #41
mahipal46 commentedin your view Use AJAX: Yes for this panel pane it will works fine.
Comment #42
labboy0276 commentedI can confirm that #32 works AND/OR you can also set use panel pane path to yes.
Comment #43
rossb89 commented#32 - Thank you very much!
Comment #44
kirkkalaAwesome, #32 fixed for me too. Thanks!
Comment #45
Marko B commentedI used #32 and it helps, but it keeps redirecting to node + cart ID. so I get something like this
http://localhost/pht/node/14/273 instead of just plain http://localhost/pht/node/14/
as if i check current path or $_GET['q'] I get this node/14/273
Comment #46
splitsplitsplit commentedHmm so the solution mentioned in 3 fixes it when I type in a filter, but doesn't fix it when I remove it. What a weird bug.
Comment #47
Arne Slabbinck commentedI used #32 with a slight improvement from an other issue thread for not getting unwanted long / weird urls and it works like a charm, thanks!
Credit goes to threads i linked!
Comment #48
lmeurs commentedIn our case when using
$view->override_patharguments got appended to the path, also see #1449248-3: When using "Override path" and an argument with "input on pane config", the argument is appended to the overridden path.To get clean URL's we used something like:
Comment #49
anvasila commentedComment #50
anvasila commentedThanks to Chris Einkauf (#3) everything is ok now!!!!
This works like charm. Block view without Page with Use Ajax to Yes.
Before this code, when i was changing the value at filter i get redirected to home page. I used this code:
Comment #51
tyler.frankenstein commented#48's
$view->override_url = drupal_get_path_alias($_GET['q']);worked for me! Usingoverride_pathwas causing any contextual filters to be appended to the URL as arguments, thus resulting in a 404 when submitting the exposed filters form.