I found it a touch awkward to have a drupal_goto in the form build function as it made it difficult to prevent this behaviour without either forking the function and replacing the callback via hook_menu_alter with a custom module, or writing a quick patch and uploaded to d.o.
In any case, my particular implementation of the election module doesn't call for a drupal_goto on the vote form if the user has already cast a vote. I'd rather set a message or handle the redirect using rules or a custom preprocess hook. This patch removes the drupal_goto and instead just hides the submit buttons (as that's the only thing available in the form array at that specific time).
There's a patch attached to this issue which can be referenced from a project make file. I don't expect any code to be ported into the main release, but if it's useful to anyone else then that's great :)
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| no-redirect-when-already-voted.patch | 1.55 KB | johan.gant |
Comments
Comment #1
pjcdawkins commentedOK.
There are a few options for this case.
election_vote_access()) to the voting page (election-post/POST_ID/vote) if the user has already voted. But this would not make it clear why the user's access is now denied.MENU_ACCESS_DENIEDfrom the page/form callback.$form['#access'] = FALSE;$form['#disabled'] = TRUE;I agree that drupal_goto() should ideally be avoided, as it is un-alterable.
There is a 7th option, of retaining the current behaviour but making the drupal_goto() alterable, with something like this:
Comment #2
johan.gant commentedHi,
Thanks for the reply. The 7th option sounds like interesting solution. The use of drupal_alter is nice as it allows the redirect to be customised. I'm happy to write a patch for this, but spare time's a bit on the short side at the moment... so might take a while to produce.
Cheers!
Comment #3
pjcdawkins commentedWell I could produce a patch for this, but first -
Can you explain why you have found the redirect to be inappropriate?
Comment #4
johan.gant commentedSure. The original redirect wasn't helpful as I couldn't override it or change its destination. With the option to control this via the form API this then gives the flexibility to manage it from other modules.
Comment #5
pjcdawkins commentedI meant - why did you want to override or change the original redirect?
Comment #6
johan.gant commentedOh, I see. The context for all this was that the project I was working on had a fairly basic set of requirements (single STV election, single post, multiple candidates) and only wanted to make the voting form for a single post (eg: election-post/x/vote) visible to non-admins. The editorial team were happy managing the election landing page and voting confirmation page as existing nodes, linking to that voting form via the rich text area. Granted, they still had to set up the election, the post and candidates to provide the data model for collecting ballots but it meant they could re-use existing functionality (metatags, images, other fields etc) and thereby avoid not have to pay for development time to theme/configure the candidate/election/post pages beyond what you start with. Access to those particular pages was restricted to administrative staff and not publicised or linked to from elsewhere.
By having a fixed redirect to go to
election_post_uri_path($post);it meant once a user had voted, they were being taken to a page that was giving them an access denied message. Instead, it would have been ideal to be able to specify a destination which could have been another node on the site or somewhere else.