0

I am trying to hide tab for user page. This is good script but work only for node type. How can I make it work for user pages? Thanks to all!

function bartik_preprocess_page(&$variables, $hook) {
    if (!empty($variables['node']) && $variables['node']->type == 'article' && in_array('editor', $variables['user']->  roles)) {
        $to_be_removed = array('node/%/edit', 'node/%/view');
        foreach ($variables['tabs'] as $group_key =>$tab_group) {
            if (is_array($tab_group)) {
                foreach ($tab_group as $key =>$tab) {
                    if (isset($tab['#link']['path']) && in_array($tab['#link']['path'], $to_be_removed)) {
                        unset($variables['tabs'][$group_key][$key]);
                    }
                }
            }
        }
    }
}

/-------- Update --------/

Now I tryed this:

function my_theme_preprocess_page(&$variables, $hook) {
    /* Hide Tabs in users page */
    global $user;
    $arg = arg();
    if (($arg == 'user') && in_array('gestore', $variables['user']->roles)) {
        foreach ($variables['tabs'] as $group_key =>$tab_group) {
            if (is_array($tab_group)) {
                foreach ($tab_group as $key =>$tab) {
                    if (isset($tab['#link']['path'])) {
                        unset($variables['tabs'][$group_key][$key]);
                    }
                }
            }
        }
    }
}

But not work.

/----- Last Update -------/

This works. Thx @Yogesh.

global $user;
if ((arg(0) == 'user') && in_array('editor', $variables['user']->roles)) {
    foreach ($variables['tabs'] as $group_key =>$tab_group) {
        if (is_array($tab_group)) {
            foreach ($tab_group as $key =>$tab) {
                if (isset($tab['#link']['path'])) {
                    unset($variables['tabs'][$group_key][$key]);
                }
            }
        }
    }
}
3
  • Its important to note that the tabs are shown because user role has permission to edit entites, if you are hiding to restrict them from updating then better you change permissions for the user role, but if you are hiding it for UI purpose then its fine.
    – arpitr
    Commented Sep 29, 2016 at 10:53
  • 1
    @Daniele, you're using $arg wrong, $arg is an array. You've to check $arg[0] == 'user' when checking first argument as user.
    – Yogesh
    Commented Sep 29, 2016 at 11:55
  • @arpitr sure, it's a change for the theme UI. I want to put a side menu instead of tab.
    – Daniele
    Commented Sep 29, 2016 at 12:03

1 Answer 1

2

You can use arg() function. The function returns a component of the current Drupal path. i.e. if you're on edit user page, the values returned by arg() function will be arg(0) as 'user', arg(1) as user ID, arg(2) as 'edit'.

function bartik_preprocess_page(&$variables, $hook) {
  $arg = arg();
  if (('user' == $arg) && (!empty($arg[1]) && is_numeric($arg[1])) && in_array('editor', $variables['user']->roles)) {
    // Your code goes here..
  }
}
6
  • Sorry I don't understand. Where should I put the user's role? In arg(2)?
    – Daniele
    Commented Sep 29, 2016 at 10:34
  • No its same like you done before put role in if condition. if (in_array('editor', $variables['user']-> roles)) { //code }
    – DRUPWAY
    Commented Sep 29, 2016 at 10:39
  • 1
    @Daniele, I've updated code to check user role.
    – Yogesh
    Commented Sep 29, 2016 at 10:54
  • @Yogesh Last question... if I wanted to set only for authenticated users (without a role)?
    – Daniele
    Commented Sep 29, 2016 at 12:15
  • 1
    You can simply check !empty($user->uid); or use user_is_logged_in() and if it returned TRUE then user is logged in else not.
    – Yogesh
    Commented Sep 29, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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