1

On admin/config/system/site-information, one can specify "Error pages" for 403 (No access) and 404 (Page not found).

It would make a lot of sense to specify different pages here, depending if you are logged in or not.

Currently my idea is to show my custom login + registration page for anonymous users, but something else (not sure yet) for logged-in users.

Is there any solution for this in contrib?

1
  • 1
    You can use a Panel Page and with conditionals shows its content blocks.
    – ropic
    Commented Apr 28, 2016 at 5:27

2 Answers 2

1

There are 2 options to do so, without any custom code involved, as detailed below.

Option 1: Use the CustomError module

The CustomError module allows the site admin to create custom error pages for HTTP status codes 403 (access denied) and 404 (not found), without creating nodes for each of them. Some more details about its features (from its project page):

  • Configurable page title and descriptions.
  • There are no author and date/time headers as with normal nodes.
  • Any HTML formatted text can be be put in the page body.
  • The error pages are themable.
  • Users who are not logged in and try to access an area that requires login will be redirected to the page they were trying to access after they login.
  • Allows custom redirects for 404s.

You'll probably be interested mostly in the part about "Users who are not logged in and try to access an area that requires login will be redirected to the page they were trying to access after they login.".

For D8 there is a 8.x-1.x-dev version for this module available also, more details about it can be found in Issue # 2219227.

Option 2: Use the Rules module

Assume the path of the "Default 403" page is set to no_access (via admin). Then create a rule using the Rules module, with as Event something like "After visiting node no_access". So that the entire rule would look something like so:

  • Events: After visiting node no_access
  • Conditions:

    1. User has role(s) - Parameter: User: [site:current-user], Roles: anonymous user
    2. NOT Text comparison - Parameter: Text: [site:current-page:url], Matching text: user/login
  • Actions: Page redirect - Parameter: URL: user/login

Should you want to do so, you could even add another Action to also display some (informational) message in the Drupal message area, with something like "You tried to visit a page for which login is required ...".

The above rule is for role anonymous. Create a similar rule for each other role you want to handle in the same way. Using the Conditional rules module you could even merge them in a single rule.

True, it might require you to enable an extra contributed module (Rules). But, as indicated by its growing popularity also, that module is probably already enabled in mostly any site (similar to the Views module), because there are dozens of use-cases for this module.

For D8 there is a 8.x-3.x-alfa version for this module available also.

3
  • Totally valid and extensive answer. Thanks! I was personally hoping for something else. But I did not express this in the question - so this clearly deserves the "accept".
    – donquixote
    Commented Apr 28, 2016 at 15:13
  • CustomError: The problem here is that you cannot simply specify paths to pre-existing pages, e.g. that were previously built with panels.
    – donquixote
    Commented Apr 28, 2016 at 15:15
  • Rules: Yeah.. I personally find that crafting rules and managing/deploying them to features is more work than just doing it in a custom module. I also somehow do not find sympathy for the language. It implies that something "happens". Whereas I tend to think of things in a static, state-based way. (I think I explained this poorly.) Still a valid answer.
    – donquixote
    Commented Apr 28, 2016 at 15:18
1

You can work with panels as suggested in the above comment.

Without panels nor any contrib module you can show blocks limited to roles on the specified error node. Review the blocks settings.

Maybe you need multiblock to place blocks several times.

4
  • Like the other answer: Definitly valid.
    – donquixote
    Commented Apr 28, 2016 at 15:20
  • Panels: Problem here is I cannot use already existing pages. Or can I?
    – donquixote
    Commented Apr 28, 2016 at 15:20
  • Blocks / multiblock: Same here, it does not let me use pre-existing pages. Also not a big friend of blocks for content. But this is more a personal preference.
    – donquixote
    Commented Apr 28, 2016 at 15:23
  • You can only use one page per code - yes. blocks or redirects to other pages also could be added programmatically in hook_page_build fe. see drupal.stackexchange.com/questions/23237/…
    – rémy
    Commented Apr 28, 2016 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.