1

My users have permission to view other users' accounts. Only the "View" tab page. This is exactly what I want.

On the user account, by default, there are three tabs:

  • View

  • Shortcuts

  • Edit

If I am user A and I go to my account, I see the tabs. If I'm user A and I go to user B's account, I don't go to his tab (it makes sense).

Now if I create a page view (e.g. bookmarks) with a menu tab, for user accounts. If I'm User A and go to User B's account, I can see the "Bookmarks" tab.

How to hide tabs on user accounts except current user account?

There is no permission to do this.

1
  • Instead of deleting a question to re-ask the same question again, you should have edited the question to make it on-topic. Asking again the same question has only the effect to make the new question be closed as duplicate.
    – apaderno
    Nov 15, 2022 at 8:38

1 Answer 1

1

You can add a RouteSubscriber with a custom access check to accomplish this. Something like:

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('entity.user.canonical')) {
      $route->setRequirement('_profile_access', '\Drupal\mymodule\Access\ProfileAccessCheck::access');
    }

    if ($route = $collection->get('entity.user.edit_form')) {
      $route->setRequirement('_user_edit_access', '\Drupal\mymodule\Access\AccountEditCheck::access');
    }

    if ($route = $collection->get('entity.user.delete_form')) {
      $route->setRequirement('_user_delete_access', '\Drupal\mymodule\Access\AccountDeleteCheck::access');
    }

    // more entries...
  }

}

and so on, and so forth.

In the access classes you can implement custom logic that should dictate visibility.

4
  • Thanks, but I don't want to code. What is the solution ? It's something that should be part of Drupal to limit access to user account tabs
    – eric55
    Nov 14, 2022 at 20:13
  • You've added a View that adds tabs to the User routes. There is no out of the box function that dynamically creates new permissions to Views routes. Even if it did, a permission only checks access to the route itself, not that you specifically have access. This is especially true since you have given access to view user profiles, so as far as the application is concerned, user/*/(tab) is permitted. This is where access checks come in.
    – Kevin
    Nov 14, 2022 at 20:42
  • Is there anything in the UI to do this without having to create some code? Or a contrib module that does this from Drupal?
    – eric55
    Nov 14, 2022 at 21:02
  • 1
    This is the second time you asked about this issue and you keep asking for module suggestions. Please understand that Drupal and the many contributed modules are a huge and versatile toolbox provided by the Drupal community. BUT it won't necessarily fit for all thinkable use cases out of the box. I've worked on hundreds of Drupal sites. None of them didn't require at least a little bit of custom programming. Unfortunately, we can't and won't do this work for you here. This Q&A site is for pointing out solutions to narrowly scoped questions. You can search for contrib modules on drupal.org. Nov 15, 2022 at 7:02

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