1

On a Drupal 8.9.13 website, I've removed the permission "View published content" from the Anonymous user. By default, when someone visits the front page, Drupal will then display:

Access denied

You are not authorized to access this page.

I want to replace this with some other text, e.g.

Please create an account to see our content.

To do this I try to create something like this in page--front.html.twig.

{% if not logged_in %}
<p>Please create an account to see our content.</p>
{% endif %}

However, I am stuck 😞.

  1. I am not able to get rid of the "Access denied" text for the anonymous user.
  2. I can't get the the text "Please create an account to see our content." to show for the Anonymous user. (If I remove "not" from the if-test it shows up as expected on the front page for the Authenticated user).

The site's theme is an otherwise unmodified subtheme of Bootstrap Barrio 5.1.4, but I don't think the problem is theme-dependent. Rather, the problem is that I don't understand the interplay between Twig and permissions (IMHO, there should be none).

3

Add a page--403.html.twig template to your theme.

See this change record: https://www.drupal.org/node/2960810

If you only want to change the default message you don't need a template. You can configure a node as access denied message in /admin/config/system/site-information.

Caveat for the non-template solution (as commented by @FreeRadical): If you restrict access to all nodes you have to find a different type of content. Could be a small module providing a custom controller which returns the static text.

BTW the themed page--403.html.twig is built in a subrequest which only job is to return a themed page with blocks. It's too late to make decisions on what to do with a 403 exception at this point. If you want to replace the core handling of a 403 exception (returning a themed subrequest) use an exception subscriber with default priority. See https://drupal.stackexchange.com/a/245211/47547

1
  • Thanks for the answer. I think there is a Catch 22 here: If you remove the permission "View published content" from the Anonymous user, configuring a node to show on 403 in /admin/config/system/site-information is not going to fly, because the Anonymous user will not be allowed to see it. You may want to add that caveat to your answer. Using page--403.html.twig works. – Free Radical Mar 30 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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