It’s easy to take Drupal’s built-in user management features for granted… out of the box, you can configure your site to allow user registrations, reset forgotten passwords, and handle user logins. We had a use case where we wanted to combine the login and registration forms into a single landing page for anonymous users, to encourage users without an account to sign up with a single click rather than hiding the registration form behind another link.
There are many ways to skin a cat in Drupal, and the more familiar you get with the way Drupal and its many contributed modules work you start to realize that it’s possible to do things like set up a combination login/registration screen with little if any custom coding.
Here’s what we did (Your mileage may vary - on some sites it may not be appropriate to use executable PHP blocks, but our permission scheme accomodated it nicely):
- Create a new block (Admin > Build > Block > Add Block). Set the input format to
PHP Code, and give it the following contents, which will display the standard user login form:<?php print drupal_get_form('user_login'); ?> - Create another new PHP Code block for the registration form:
<?php print drupal_get_form('user_register'); ?> -
Install the Panels module, and add a new Panel using the two column layout.
-
Put the login form block in the left column and the registration form block in the right column. Give the panel a path like
myapp/register- it’s very important that the second part of the path contain the wordregister. (More on that in a moment) Save the panel. -
Go to
Administer > Site Configuration > Error Reportingand set the default 403 (Access Denied) error page to the path of the Panel you just created. This means that whenever an anonymous user encounters a page requiring authentication, they will get the login/registration form instead of the somewhat rude, default “Access Denied” error.
And that’s it. Zero custom code required, and we had our combination login/registration screen ready to go. We did choose to write a small module implementing hook_form_alter() to streamline the forms a little bit, making fields shorter, eliminating some of the field descriptions, and adding a link to the password reset form. Combined with some CSS styling, the result is pretty sharp:
So, why does the Panel’s path have to have two items, the second one always being “register”? The answer is in the core user.module file, in the _user_edit_validate() function:
<?php
// Validate the username:
if (user_access('change own username') || user_access('administer users') || arg(1) == 'register') {
if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error);
}
else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $uid, $edit['name'])) > 0) {
form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name'])));
}
else if (drupal_is_denied('user', $edit['name'])) {
form_set_error('name', t('The name %name has been denied access.', array('%name' => $edit['name'])));
}
}
?>This chunk of code is responsible for making sure a submitted user name isn’t already taken, and the only way it gets called when the user_register form is submitted is if arg(1) is “register”, as is the case when signing up from the standard /user/register path. If you give your combination login/register panel a path whose second item is not “register”, it will still work, but the system won’t catch duplicate user name requests, resulting in the behavior reported at http://drupal.org/node/148525.



I could be wrong, but isn't
I could be wrong, but isn't this easy to do with panels?
core patch
I would like to see this incorporated into the default 403 page in core.
Thanks...
Thanks for the good write-up. This should should certainly be part of Drupal core and the default 403 page.
Great idea
Thanks for sharing this - it's a great idea.
I hesitate to ask for more when you've been generous in sharing this suggestion, but would you mind sharing the code you used in the small custom module to change the layout of the forms?
My username and password fields are way too long and overlap onto the other panel column. I guess that's what you're saying your module fixes.
Understood if you don't want to share it as well, but if you do - thanks!
+1 for core in some form or other (presumably without panels requirement)
cheers
Steve
I love this tutorial.Great
I love this tutorial.Great for usability! Any chance we could get your hook form alter code? Thanks again!
Yeah, it is really helpful
Yeah, it is really helpful and I managed to do it today!
Getting Error when i had created login and registration page
Hi,
Please find the below error show in the browser.
Redirect Loop
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.
* Have you disabled or blocked cookies required by this site?
* NOTE: If accepting the site's cookies does not resolve the problem, it is likely a server configuration issue and not your computer.
Without Panels FYI
FYI
I also did this for an intranet page using tables with the PHP in each table on a page instead of panels if your not comfortable installing panels. I know that's obvious to most but just in case..... : )
Post new comment