0

I'm currently setting up a simple LMS in Drupal 8 and would like to customize the Access Denied page as follows:

  • Anonymous users should be redirected to the Login page, when they hit a 403 error.
  • Logged in users trying to access a specific content type (a Course), which they are not allowed to access, should be redirected to a custom message (telling them to buy the course or something like that). It would be even better, if they could be shown the teaser of that node, but that's not a necessity.
  • In all other cases, I would like to show a general Access Denied page.

Fwiw, there were similiar questions for D6 here and here that also didn't receive any good replies.

Does anybody happen to know a good solution (for D8)? Any help would be much appreciated.

3
  • 2
    Use an exception subscriber, D8 provides a base class HttpExceptionSubscriberBase where you can put the code in on403(), see drupal.stackexchange.com/questions/165884/….
    – 4uk4
    Commented Oct 14, 2018 at 15:29
  • 1
    What!? I've been on this platform for quite a while (in different sections) and I do know the rules, but I'm completely at a loss, as to why my question is supposed to be "primarily opinion-based"? I'm looking for help solving a specific problem, not for ideas as to which module is the coolest... (And why hasn't the question that I mentioned been put on hold? It's virtually the same question, except it's for D6.)
    – Marcus C.
    Commented Oct 14, 2018 at 20:39
  • @4k4: Thanks for your suggestion. This sounds like a more complex route and, frankly, I was hoping for a slightly simpler solution. But I will try your idea as soon as possible and will report back. Thanks again.
    – Marcus C.
    Commented Oct 14, 2018 at 20:45

1 Answer 1

1

You could do that using the rules module.

Create a rule using the Rules module, with as Event something like After visiting node no_access. So that the entire rule configuration would look something like this:

Events: After visiting node no_access

Conditions: User has role - Parameter: User: [site:current-user], Roles: anonymous user NOT Text comparison - Parameter: Text: [site:current-page:url], Matching text: user/login

Actions: Page redirect - Parameter: URL: user/login

And then for the logged in user, you can create another rule that checks if the user is logged in, and then redirects to a custom page if the user visits a no_access page.

For the general access denied page, you can set a url in Admin > Configuration > System > Site information, under error pages.

Hope it helps

5
  • Thanks for the quick reply. That sounds great and simple, will try that. I didn't know that the Rules module is already usable for D8...
    – Marcus C.
    Commented Oct 14, 2018 at 15:27
  • Cool, glad that you could use the help :)
    – user72672
    Commented Oct 14, 2018 at 15:30
  • Please write if you get it to work
    – user72672
    Commented Oct 14, 2018 at 15:56
  • I just tried to create the rule, but unfortunately I'm already failing at the first stage: There doesn't seem to be an event like "after visiting node no access". Am I missing something? Maybe this event is still missing in the D8 version?
    – Marcus C.
    Commented Oct 14, 2018 at 16:44
  • Well, this issue is for drupal 7, but maybe something might be useful, drupal.org/project/rules/issues/796056
    – user72672
    Commented Oct 14, 2018 at 21:08

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