How to hide a local task (tab) in Drupal?
Quite often we want to have a parent tab to be selected, but the sublevel tasks are not displayed or we just want remove completely some tabs for user of specific roles.
There are two ways to do it:
The first option is to use hook_menu_local_tasks_alter
function your_module_local_tasks_alter(&$local_tasks) {
unset($local_tasks['id_of_local_tasks']);
}
This code will remove your tab completely from the system, it means it will not be displayed anywhere, but sometimes we want to hide the tab only on the specific pages
The second option is to use hook_menu_local_tasks_alter
function your_module_menu_local_tasks_alter(&$data, $route_name) {
$routes = ['entity.node.canonical']; // add your routes to this array and your tab will be hidden for this routes
if (in_array($route_name, $routes)) {
unset($data['tabs'][0]['id_of_local_task']);
}
}