What happens when a user bookmarks an access-protected page? If their session expires before they next visit the bookmark, they'll see an "Access Denied" message with no login form. How confusing for the end user! Let's change that.

One good option is to use the Redirect 403 to User Login module. However, you can also accomplish similar functionality with a simple Panels page (and avoid the overhead of another module). There are two steps. First create the new Panels page to replace the standard "Access Denied" page (steps and export code below). And second, set Drupal's "Default 403 (access denied) page" setting to use the newly created Panels page.

Step 1/2: Create the Panels Page

Before following the steps below, you can try starting with the export code in this gist. Visit the import page and paste in the code.

Create a new custom page.

Give it a title and a path.

Create a new variant for the un-authenticated (anonymous) 403 page. Give it a selection rule on "User: role" and select "Anonymous" as the role.

Add the "User login" widget to the variant's content section.

Create a second variant, for authenticated traffic.

This time, no selection rules are needed. Simply add a "New custom content" pane to the content section.

Give the custom content pane some text. E.g., "You are not authorized to access this page."


Step 2/2: Set Drupal's default 403 (access denied) page

Visit the Configuration » System » Site Information page.

And set the "Default 403 (access denied) page" to the URL you used above for you Panels page.


Voila! Try visiting an access-restricted page in an anonymous browser, and you'll see the login block. After logging in, you'll either see the requested page (if that user has access) or the "access denied" message (if the user doesn't have access).

Additional Resources

You Stay Classy Panels Module | Webinar
Write Better Code with EntityMetaDataWrappers | Mediacurrent Blog Post