1

I'm hoping someone can take pity on me because I just can't seem to figure out how to make a simple module to remove, or hide some tabs in my user pages. I read the docs on making custom modules, questions about removing tabs, and thought I figured it out. I made a custom module called 'TabEdit' with this structure, just to test it out:

/tabedit

    tabedit.info.yml
    tabedit.module

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
/**
 * Implements hook_menu_local_tasks_alter()
 */
function tabedit_menu_local_tasks_alter(&$data, $route_name) {
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
}

It didn't work, so I obviously still don't get the fundamentals on how to make a module. What I want to do is remove the Shortcuts, Simplenews Subscriptions, Activity, and Submissions from the user menu tabs.

2

Hooks need to be written in . module file not in controller. Move your code to .module file and also it should be tabedit_menu_local_tasks_alter

tabedit.module

<?php

/**
 * Implements hook_menu_local_tasks_alter()
 */
function tabedit_menu_local_tasks_alter(&$data, $route_name) {
  unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']);
}
  • Clear flush all caches so hook gets pickedup.
| improve this answer | |
  • So I would take what was in the php file and move it to a tabedit.module file? If that's the case, do I need anything in the Controller folder? – TomP Sep 27 at 17:51
  • @TomP no Controller folder needed. – No Sssweat Sep 28 at 2:46
  • I had made those changes. I just edited my original question to match. My user tabs still has NEWSLETTERS (/user/2/simplenews). I've also flushed the cache after reinstalling each time. I'm hoping if this can work I can use it to get rid of a couple other tabs as well. – TomP Sep 28 at 12:53
  • @TomP file name should be tabedit.module not RemoveTabEdit.php – miststudent2011 Sep 28 at 13:42
  • Sorry. I edited the original question to match what I've been trying – TomP Sep 28 at 19:14
1

A file like src/Controller/RemoveTabEdit.php is for a class, not a function. In the specific, it's not for hook implementations, which should always go in the module file.

The src/Controller/RemoveTabEdit.php file is automatically loaded when the RemoveTabEdit class is used, but since that doesn't happen, the file is never loaded and that hook implementation is never found.

As side note, it's better to avoid uppercase characters in the module machine name. While PHP is case insensitive, the code looking for hook_update_N() implementations is still case sensitive.

| improve this answer | |
  • Ok that starts to make sense. So what would the structure look like. Doesn't a .module file have to go in the root of the module? Also what would the php file need to do then? – TomP Sep 27 at 18:07
  • If you don't have any code for that file, that file can simply be removed. Yes, the tabedit.module file goes in the directory containing the tabedit.info.yml file. – kiamlaluno Sep 27 at 18:16
  • I've now have a single folder 'tabedit' with two files the 'tabedit.info.yml' and 'tabedit.module'. I completely removed the src folder. The .module file only has the one function I'm using to test <?php 'function tabedit_menu_local_tasks_alter(&$data, $route_name) { unset($data['tabs'][0]['simplenews.newsletter_subscriptions_user']); }' It's still not working. If the module is correct maybe the tab address is wrong. – TomP Sep 27 at 19:00
  • If you edit a module code after it has been installed, Drupal doesn't notice the code has been changed, or new files have been added. You need to uninstall the module and re-install it. Any code change must be done when the module isn't installed. As for the SimpleNews module, it has a simplenews.newsletter_subscriptions_user tab; that is correct. – kiamlaluno Sep 27 at 19:19
  • In particular, Drupal keeps in cache the list of hooks implemented from modules. The only way to refresh that cache is installing or uninstalling a module. – kiamlaluno Sep 27 at 19:21
0

This did finally works. I renamed it tabedit.module and made the code

<?php
/**
  * Implements hook_menu_local_tasks_alter().
  */
function tabedit_menu_local_tasks_alter(&$data, $route_name) {
/*      If a current user has the 'administrator' role then do not hide the tabs.
    $current_user_roles = \Drupal::currentUser()->getRoles();
    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']);
}
``
| improve this answer | |

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.