First off, great module, this is exactly what we needed for our client, so thanks bojanz.
Summary of Problem
I needed to have an order form on the product page that shows each product variation on it's own line with a quantity field and an add to cart button. To start, I created a page display view to test things out which I got working correctly. I was able to add any number of any product variation to the cart using the page display of the view. I was redirected to the same page (as expected) and was shown the add to cart confirmation popup.
I then created a block display so I can have this order form on the individual product pages. To limit the product variations to only those related to the product being viewed, I created a contextual filter of the Nid and provided a default value (since this is a block display, there is no source for contextual filters) of "Content ID from URL".
The block displays correctly, however, the form action is incorrect, instead of using the aliased URL of the current page as I would expect, the form action was set to "/node/nid/nid". When the add to cart button was clicked, the page refreshed, and I was brought to the product page at "/node/nid/nid" instead of the aliased version of the page that I clicked "Add To Cart" on. I was not shown the add to cart confirmation popup, and my cart is empty. I have attached the view export if it would be helpful to see.
Proposed Solution
I searched through the issue queue and identified this patch that enables the block display, and the use of views_embed_view. While looking at the code I was able to identify the following line as the issue:
$form['#action'] = url($view->get_url(), array('query' => $query));
Which updates the form action by using the views method get_url and appending any query string parameters if they were present. The problem is that the $view->get_url() is returning the above mentioned url ("/node/nid/nid").
The fix for me was to use the request_uri() function to get the aliased URL for the page current being viewed. I switched the above line with this:
$form['#action'] = url(trim(request_uri(),'/'), array('query' => $query));
I am using trim to remove the leading (and trailing if present) slash. Once this switch was made, my block display view began working correctly. I am not sure if there are any unknown effects of using the request_uri instead of the $view->get_url().
EDIT: The above does not work correctly when there are special characters in the URL, so I have switched to using
$form_action = drupal_get_destination();
$form['#action'] = url($form_action['destination'],array('query'=> $query,'html' => TRUE));
The above returns the correct path "node/nid".
To Do
For my purposes this appears to work just fine, I will be playing around with it some more over the next couple of days and will update if I find any issues with the switch.
Comment | File | Size | Author |
---|---|---|---|
#10 | incorrect_form_action-1985340-10.patch | 988 bytes | mglaman |
#7 | incorrect_form_action-1985340-7.patch | 1.02 KB | mglaman |
Comments
Comment #1
T.Mardi CreditAttribution: T.Mardi commentedI can confirm this change works.
Before changing the line, if I left quanties at zero and hit 'add to cart' I was getting redirected to "/node/nid/nid". Now it shows the correct url of the node being viewed.
Thanks.
Comment #2
gdarends CreditAttribution: gdarends commentedI seem to have the same issue.
My setup is as follows:
I created a block view.
Then I used viewfield to attach the view to the product display content type.
Is it not safe to always set the
action
, instead of only setting it specifically for block andviews_embed_view()
?In my case I also get the same issue, but my case does not meet the conditional block. (
$view->override_path
is not empty) This then does not set theaction
, but still gettingnode/nid/nid
.Comment #3
gdarends CreditAttribution: gdarends commentedAlso, I'm not sure what the optional
html => TRUE
in the array is for, but I haven't seen anything in the url api documentation about it.Comment #4
gdarends CreditAttribution: gdarends commentedI propose the following patch. Which should work regardless of wether it's a page view or block/views_embed_view().
Was tested as a page view and also in my case as a block view attached to a node type using viewfield module.
Comment #4.0
gdarends CreditAttribution: gdarends commentedUpdating with latest insight into issues using request_uri().
Comment #5
gdarends CreditAttribution: gdarends commentedComment #7
mglamanFixes issue. Attaching re-roll so it applies to latest dev.
Comment #8
coatezy CreditAttribution: coatezy commentedAny idea when this patch might be merged?
Comment #9
joelpittetSeems when you have filters added to your view, this patch will break the form URL.
Knocking it back to needs work.
I think it's because form_action['destination'] includes the previous query arguments.
Comment #10
mglamanTo be honest I'm not sure why I didn't just use current_path. This should fix it.
Comment #11
maxplus CreditAttribution: maxplus commentedHi,
#10 works great for me, my URL now keeps the same after a add to cart action.
Before patch #10, my URL always got longer and longer after every add to cart action...
Comment #12
joelpittet@maxplus, would you mind setting the status to RTBC(reviewed and tested by the community) if you've tested it and it's working. I'll gladly commit this.
Comment #13
maxplus CreditAttribution: maxplus commentedComment #15
joelpittetThanks i've pushed to to -dev.