I want to hide simplenews, activity and submissions tabs on user pages. I see that was a module to do that (Tab Tamer) but it was designed to Drupal 7 and I'm in D8.

I tried various alters form codes in module without sucess. Any kind of help was very appreciated.

Comments

wombatbuddy’s picture

You can acomplish this with the following hooks:
hook_menu_local_tasks_alter(),
hook_local_tasks_alter().
For instance, you can remove the 'Edit' tab like this: 

/**
 * Implements hook_menu_local_tasks_alter().
 */
function my_module_menu_local_tasks_alter(&$data, $route_name) {
  // Remove 'Edit' tab from the user page.
  unset($data['tabs'][0]['entity.user.edit_form']);
}
renguer0’s picture

I see that but do you know how to see the name of other tabs that I've mentioned in order to hide them?

Thanks!

wombatbuddy’s picture

I prefer to explore variables using kint() (the 'Devel' module is needed).
For instance: 

kint($data['tabs']);
renguer0’s picture

Sorry but I didn't used any debug before. I installed this module and enabled it.

Where I can put this command?

Thanks for your time.

wombatbuddy’s picture

In your case you need to explore the structure of '$data['tabs']' array.
Therefore put it inside the hook I wrote above, like this: 

function my_module_menu_local_tasks_alter(&$data, $route_name) {
  kint($data['tabs']);
}

or like this: 

function my_module_menu_local_tasks_alter(&$data, $route_name) {
  kint($data['tabs']['0']);
}

(Also sometimes is useful to insert 'die' after kint(), to stop a script running).

For more details see 'How to Print Variables using Kint in Drupal 8'.

renguer0’s picture

Oh yeah! That's I was looking for. To also contribute some from my side, I attach the code for everyone that will looking for:

function MODULE_menu_local_tasks_alter(&$data, $route_name) {
  unset($data['tabs'][0]['entity.node.version_history']);
  unset($data['tabs'][0]['entity.webform_submission.user']);
  unset($data['tabs'][0]['tracker.user_tab']);
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
}

This code hides Revisions (in nodes), Submissions, Activity and Suscriptions tabs (in user page). Of course, this must be placed in MODULE.module file and MODULE needs to be changed to the module that executes this function.

Thanks for the explanation wombatbuddy, I really searched before and I didn't find this info so clearly.

manishdrupaldev’s picture

Yes, It worked for me.

Thanks!

wxman’s picture

I know this is an older post, but I'm trying to do this as well without any luck.

I have made a custom module called 'tabedit' made up of only two files: tabedit.info.yml and tabedit.module.php.

tabedit.info.yml

name: TabEdit
description: Edit tabs shown in user pages.
type: module
package: custom
core: 8.x
core_version_requirement: ^8 || ^9

tabedit.module.php
 

<?php
/**
 * Implements hook_menu_local_tasks_alter()
 */
 
function tabedit_menu_local_tasks_alter(&$data, $route_name) {
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
}

this is just for a test to see if I can remove the Simplenews 'Newsletters' tab. I would also like to get rid of 'Shortcuts', 'Activity', and 'Submissions'. I have tried all the suggestions but nothing seems to be able to remove the tabs. I'd love to find out just what I'm missing.

wombatbuddy’s picture

@wxman, try to rename the 'tabedit.module.php' file to the 'tabedit.module'

wxman’s picture

@wombatbuddy, That was a big help! I got rid of the .php and tried this:

function tabedit_menu_local_tasks_alter(&$data, $route_name) {
  unset($data['tabs'][0]['tracker.user_tab']);
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
}

That removed the Activity, and Newsletter Subscriptions tabs. Now if I could just find the address of the 'Shortcuts', 'Activity', and 'Submissions' tabs I'd be golden. Thanks for the suggestion.

wombatbuddy’s picture

@wxman, here is the implementation: 

/**
 * Implements hook_menu_local_tasks_alter().
 */
function YOUR_MODULE_menu_local_tasks_alter(&$data, $route_name) {
  $current_user_roles = \Drupal::currentUser()->getRoles();
  // If a current user has the 'administrator' role then do not hide the tabs.
  if (in_array('administrator', $current_user_roles)) {
    return;
  }

  unset($data['tabs'][0]['tracker.user_tab']);
  unset($data['tabs'][0]['shortcut.set_switch']);
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
  unset($data['tabs'][0]['entity.webform_submission.user']);
}

But take a note, that hiding of the tabs does not prevent a user from visiting pages. For instance, if a user enter the path like this:
/user/2/simplenews
then he be able to open the ''Newsletters" page.

 

wxman’s picture

@wombatbuddy, Thanks again, that did the trick. I was looking everywhere to find how to address those tabs but wasn't having any luck.

alpha-1’s picture

Hi,

I am trying to hide ldap help redirect page but hook menu alter doesn't hide that for authenticated users. Right now logged in users can see log in help tab which doesn't makes sense. Can someone please help?

drupal version 9.2.10

Thanks

wombatbuddy’s picture

@alpha-1 if you share the step to reproduce this case we will try to help.

alpha-1’s picture

thanks for replying.

I am using ldap module for drupal 9 and under 'LDAP Authentication settings' it has a field LDAP Account User Help URL (URL to LDAP user help/documentation for users resetting passwords etc. Should be of form http://domain.com/. Could be the institutions LDAP password support page or a page within this Drupal site that is available to anonymous users.)

Now this page is showing in tabs on /user/username page for all the logged in users, I have tried above codes with current role  'authenticated' as well,  clear cache but it didn't work. It works for shortcut, edit tabs 

function tabedit_menu_local_tasks_alter(&$data, $route_name) {
  $routes = ['entity.user.canonical'];
  if (in_array($route_name, $routes)) {
    unset($data['tabs'][0]['ldap_authentication.show_user_help_link']);
  }
}
wombatbuddy’s picture

/**
 * Implements hook_menu_local_tasks_alter().
 */
function YOUR_MODULE_menu_local_tasks_alter(&$data, $route_name) {
  if ($route_name == 'entity.user.canonical') {   
    unset($data['tabs'][0]['ldap_authentication.show_user_help_link:ldap_authentication.show_user_help_link']);
  }
}
alpha-1’s picture

thank you so much, it works as expected! 

['ldap_authentication.show_user_help_link:ldap_authentication.show_user_help_link']