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

Chris Einkauf’s picture

Category: bug » support

I 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:

function theme_form($element) {
  // Anonymous div to satisfy XHTML compliance.
  $action = $element['#action'] ? 'action="'. check_url($element['#action']) .'" ' : '';
  return '<form '. $action .' accept-charset="UTF-8" method="'. $element['#method'] .'" id="'. $element['#id'] .'"'. drupal_attributes($element['#attributes']) .">\n<div>". $element['#children'] ."\n</div></form>\n";
}

And here is the new code after I modified it and stuck it into my template.php:

function mabtheme_form($element) {
  //Anonymous div to satisfy XHTML compliance.
  if($element['#id'] = 'views-exposed-form-browse-default') {
  $action = 'action="/browse" ';
  }else {
  $action = $element['#action'] ? 'action="'. check_url($element['#action']) .'" ' : '';
  }
  return '<form '. $action .' accept-charset="UTF-8" method="'. $element['#method'] .'" id="'. $element['#id'] .'"'. drupal_attributes($element['#attributes']) .">\n<div>". $element['#children'] ."\n</div></form>\n";
}

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 "/"?

merlinofchaos’s picture

Status: Active » Fixed

This 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().

Chris Einkauf’s picture

Thank 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):

$view = views_get_view('browse','default');
$view->override_path = $_GET['q'];
$viewsoutput = $view->preview();
print $viewsoutput;
JirkaRybka’s picture

For 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.

harkaman’s picture

Hi 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.

Chris Einkauf’s picture

Harkaman: 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).

harkaman’s picture

eink21:

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!

Chris Einkauf’s picture

When 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.

harkaman’s picture

Hi 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.

Chris Einkauf’s picture

Well 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.

Chris Einkauf’s picture

See 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.

harkaman’s picture

Thank you so much for the code!

Chris Einkauf’s picture

You'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.

Status: Fixed » Closed (fixed)

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

alextronic’s picture

Yes, 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.

alextronic’s picture

Status: Closed (fixed) » Active

'Active' because it's important to know about how to obtain a clean URL with the provided code.

dawehner’s picture

Did you tryed out

$view = views_get_view('browse','default');
$view->override_path = $_GET['q'];
$viewsoutput = $view->preview();
print $viewsoutput;

??

Perhaps try out


<code>
$view = views_get_view('browse','default');
$view->override_path = implode("/", arg());
$viewsoutput = $view->preview();
print $viewsoutput;
alextronic’s picture

Thanks 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.

dawehner’s picture

What is your definition of "long url".

You didn't described yet, what this is.

alextronic’s picture

Yes, 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-group is the alias path to the node. The rest is filters selected in the View.

JirkaRybka’s picture

That'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.

alextronic’s picture

You'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.

Chris Einkauf’s picture

As 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.

merlinofchaos’s picture

Status: Active » Closed (works as designed)

Sorry, this is By Design with Views, as mentioned earlier. Those URLs can be shortened somewhat by making nicer identifiers in the filter settings, though.

rickh’s picture

I 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.

asiby’s picture

Title: views_embed_view() + exposed filters button = homepage redirect » You are not using the right syntax.

This is very strange. the syntax of views_get_views() is ... (http://drupalcontrib.org/api/function/views_get_view/6)

5 views_get_view($view_name)
6 – 7 views_get_view($name, $reset = FALSE)

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 ...

...
$view = views_get_view('browse','default');
$view->override_path = $_GET['q'];
$output = $view->execute_display('default', array($argument1, $argument2, ...));
...

Cheers

Maestro

asiby’s picture

Title: You are not using the right syntax. » views_embed_view() + exposed filters button = homepage redirect

Sorry guys, I have accidentally changed the title of this ticket. I am putting it back.

jisuo’s picture

I added this code to my tpl file

$view = views_get_view('Private_Cottages', 'default');
$view->override_path = $_GET['q'];
$viewsoutput = $view->preview();
print $viewsoutput;

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.

fourmi4x’s picture

Hi,
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!

henrijs.seso’s picture

One more thing for people using "Content pane" display in views. You need to set "Use Panel path: Yes" in order to fix this issue.

ikeigenwijs’s picture

Looked ages for this. thx

goron’s picture

Extremely 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:

/**
 * Implements hook_views_pre_build().
 */
function <mymodule>_views_pre_build(&$view) {
  if ($view->name == '<view_name>') {
    // Set the override path to the current page. This will redirect the back view to
    // the current page when submitted in case AJAX fails.
    $view->override_path = $_GET['q'];
  }
}
goron’s picture

Wouldn'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?

beto_beto’s picture

hello 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.

timmetj’s picture

Version: 6.x-2.6 » 7.x-3.5

The 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

mazdakaps’s picture

i 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

mazdakaps’s picture

well 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

aendra’s picture

Re: #30 -- Thanks a tonne, mansspams! That totally solved my issue with Views Content Panes. Would've been looking for that forever otherwise!

stevieb’s picture

#32 worked sweetly for me - thanks

mpotter’s picture

goron: Thanks very much! #32 worked for me with my ajax'd content panes. Just #30 didn't help.

mahipal46’s picture

in your view Use AJAX: Yes for this panel pane it will works fine.

labboy0276’s picture

I can confirm that #32 works AND/OR you can also set use panel pane path to yes.

rossb89’s picture

#32 - Thank you very much!

kirkkala’s picture

Awesome, #32 fixed for me too. Thanks!

Marko B’s picture

I 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

if ($view->name == 'commerce_cart_block') {
    dpm(current_path());
    $view->override_path = current_path();
  }
splitsplitsplit’s picture

Hmm 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.

Arne Slabbinck’s picture

I 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!

  if ($view->name == 'cart_summary_small'){
    $view->override_url = base_path() . $_GET['q'];
  }

Credit goes to threads i linked!

lmeurs’s picture

In our case when using $view->override_path arguments 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:

function MYMODULE_search_views_pre_build(&$view) {
  if (!$view->get_path() && !$view->get_url()) {
    $view->override_url = drupal_get_path_alias($_GET['q']);
  }
}
anvasila’s picture

anvasila’s picture

Thanks to Chris Einkauf (#3) everything is ok now!!!!

<?php
// Correct Code
$view = views_get_view('MY_VIEW','default');
$view->override_path = $_GET['q'];
$viewsoutput = $view->preview();
print $viewsoutput;
?>

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:

<?php
// Old Code. Not working Ajax
$view100 = views_get_view('MY_VIEW');
$view100->execute();
print $view100->render();
?>
tyler.frankenstein’s picture

#48's $view->override_url = drupal_get_path_alias($_GET['q']); worked for me! Using override_path was causing any contextual filters to be appended to the URL as arguments, thus resulting in a 404 when submitting the exposed filters form.