I created a Twig template for the user account /user/ID page on Drupal 8.

How to display this template only if it is the account page of the current user?

For example if I go to the page of my account, I can display the page. If I go to another user's account page, a 403 error should be displayed.

  • 1
    Welcome to Drupal Answers! We love to help you. What you are asking for, it has nothing to do with Twig or templates in the first place. So I reworded your question and removed the Twig template from it. – leymannx Nov 18 '18 at 16:16
  • 1
    Uncheck the permission that deals with viewing user profiles? That should solve it? Unless that affects the current user too. – Kevin Nov 18 '18 at 17:10

Go to admin path : /admin/people/permissions and uncheck the permission named 'View user information' for the concerned roles, then save.

This does not affect users when they are on their own account page.

I did the following test, created two new users (4 and 5 in my case) :

  • user 4 can access user/4 page but has "Access denied" for user/5 page.
  • user 5 can access user/5 page but has "Access denied" for user/4 page.
  • both users 4 and 5 have "Access denied" for user/1 page.

Your Answer

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