Drupal - How to add / remove tabs from user profile?

If you don't want to use a contrib module like TabTamer then, In Drupal 7, you can use hook_menu_alter to disable the MENU TAB by using access callback


    /**
     * Implementing hook_menu_alter
     * Restrict MENU TABS access
     */
    function MODULE_NAME_menu_alter(&$items) {
        if(/* check your conditions */) {
            $items['user/%user/hybridauth']['access callback'] = FALSE;//Change the MENU PATH as per your requirement.
        }
    }

Note

  • Get the MENU PATH by printing $items variable.

You might be looking for the Tab Tamer module:

Tab Tamer is an administration utility that provides easy re-ordering, hiding, and deactivation of tabs and subtabs. You can also rename tab labels.


For Drupal 8, I was able to address this in 2 ways:

Change the name of a user account tab item / remove a tab item with hook_menu_local_tasks_alter:

/**
 * Implements hook_menu_local_tasks_alter().
 */
function MYMODULE_menu_local_tasks_alter(&$data, $route_name) {
    if ($route_name == 'entity.user.canonical') {
        foreach ($data['tabs'][0] as $type => &$tab) {
            // Replace the "View" tab name with "Hola"
            if ($type == 'entity.user.canonical' && $tab['#link']['title'] == 'View') {
                $tab['#link']['title'] = 'Hola';
            }
            unset($tab);
        }
        // Hide the "Edit" tab entirely
        unset($data['tabs'][0]['entity.user.edit_form']);
    }
}

One thing to note is that this doesn't actually make the edit user link inaccessible -- it merely hides it from the tabs at the top of the user profile. You'll need to implement routing to actually hide something.

Add a new tab item, in this case a "Status" form:

mymodule.links.menu.yml:

mymodule.account_status:
  title: 'Status'
  parent: 'entity.user.canonical'
  route_name: 'mymodule.account_status'

mymodule.links.task.yml:

mymodule.account_status:
  title: 'Status'
  route_name: 'mymodule.account_status'
  base_route: 'entity.user.canonical'

mymodule.routing.yml:

mymodule.account_status:
  path: '/user/{user}/status'
  defaults:
    _form: 'Drupal\mymodule\Form\AccountStatusForm'
    _title: 'Status'
  requirements:
    _permission: 'access content'
    user: \d+

mymodule/src/Form/AccountStatusForm.php:

namespace Drupal\subscription\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Account status form
 */
class AccountStatusForm extends FormBase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'mymodule_account_status_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {

        // TODO: Build form

        return $form;
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {

        // TODO: Handle submit

    }

}

This answer is pretty detailed so I'll leave it to folks to go through the D8 docs if anything is unclear. At minimum, you should understand the process of using custom forms (and/or controllers depending on what you need here) to implement this code.

Tags:

Routes

Users

7