Be the first to know about special offers and new products for web developers and Internet business owners.
I will never share your email address with the bad boys. Period.
|
Be the first to know about special offers and new products for web developers and Internet business owners. I will never share your email address with the bad boys. Period.
The latest
Recent comments
|
Redirecting Users After Submitting a Form in Drupal 5
Submitted by alexis on Thu, 2007-04-05 15:14.
If you create web sites based on Drupal you know that controlling forms, how they are themed, validated and processed, is a highly valued skill for any serious developer. This article will discuss a few ways to redirect your users to another page after they've submitted a form, overriding Drupal's defaults. Drupal 5 provides developers with a Forms API, a clever way to craft HTML forms using logical and extremely flexible arrays. Drupal Forms API also allows developers to theme every little detail of a form. For simpler cases using the #prefix and #suffix properties can be enough, but if you need extreme power, and sooner or later you will, then you can use forms-exclusive theme functions and drupal_render(). I'll explain the basics of Forms API in a future article, if you can't wait I suggest taking a look at the Forms API Quickstart Guide, for now let's focus in redirecting users after form submission in Drupal. In the beginning was the form functionEvery form in Drupal starts as a function that creates an array, usually named $form. Then the function's name is passed to drupal_get_form(). As every function in a module, a form creation function should start with the name of the module, for example:
First, this assumes my module is named boogeeks, the first word in my function's name. I'm separating each word with an underscore and I suggest you do the same. Second, I've chosen the verb notify to have a clear idea of what the form will do. I used something like this in a recent project for hacking a form where users opt-in to receive email alerts. Third, I added form, helpful when you have many functions in your module and need a quick way to tell apart the ones creating forms. Notice that the only requirement for naming your form function is the first one, starting with your module name, the other two are my suggestions for cleaner and easier to understand code. Theme, validate and submitNow that we have boogeeks_notify_form() taking charge of form creation we need a few additional functions, all of them named based on the original.
For this article's purposes we are interested in the third function, the one ending in _submit. Where to go now?Every form submission function, boogeeks_notify_form_submit() in our example, needs to return a value, this value is the url where the user will go after successfully submitting a form. So, if you want to thank your user after opting in to receive email alerts then you can finish boogeeks_notify_form_submit() with something like:
That's the url of some page with the usual thank-you-we-love-you-dear-visitor stuff. All Drupal provided forms have pre-defined values in their submit functions. Two well known and often used ones are user_login() and user_register(), used to create new accounts, both are created in user.module. How to override default form redirection in DrupalThere are two ways to override default form redirection, with a destination parameter in a url and using the #redirect property of Forms API. After some testing, I know this could be obvious to hardcore Drupal developers but it's still useful for new comers, I determined the redirection processing flow is as follows:
You'll need to modify a form using hook_form_alter() to use #redirect. If you want to work with destination you'll have to include it in the url used to call the page showing your form. Now you can take your users whenever you want after submitting a form, no matter if it's a Drupal provided form or one that you've coded. Bonus: got a headache with redirection after registration?Yeah, me too, at least until finding a small bug in user.module, maybe that's your case. Trackback URL for this post:http://www.ventanazul.com/webzine/trackback/44
|
Want to launch a online store, need a new look for your website, market your services on the Net or build a virtual community? Not a problem, let's talk about it. Where To Host Your Sites? |
URLs are not paths
You mix up Drupal menu paths are URLs throughout the article. They are completely different things.
URLs and menu paths
Hi Steven, thank you for commenting. Now I'm a little confused with the terms.
If I correctly understand menu paths are the ones created from hook_menu and often related to a callback function, right? Let's say user/login and user/register in user_menu(), right?
What's what the return value in a _submit function, the destination parameter and $form['#redirect'], a url, right?
Please let me know what I'm missing in order to correct the article and be as accurate as possible.
Regards!
Alexis Bellido
workflow_ng
the new Workflow-ng can do the same job, easier for non-programmers.
Post new comment