2

I am trying to hide node "edit" tab for certain content types and roles without removing or disabling access to the edit pages. An edit button is going to be created manually and displayed on node pages.

Used below snippet but it disables access to the edit pages too.

/**
* Set up a new permission for tabs.
* Grant access under admin -> user management -> permissions
*/
function mymodule_permission() {
  return array(
    'view hidden tabs' => array(
      'title' => t('Node tabs visibility'),
      'description' => t('Grant access to node tabs.'),
    ),
  );
}

/**
* Implementation of hook_menu_alter().
* Show/hide tabs according to permission settings.
*/
function mymodule_menu_alter(&$items) {
$items['node/%node/edit']['access callback'] = 'user_access';
$items['node/%node/edit']['access arguments'] = array('view hidden tabs');
}

Also tried using this code, which kinda works but disables edit tab on all content types and for all roles.

function mymodule_menu_alter(&$items) {
$items['node/%node/edit']['type'] = MENU_CALLBACK;
}

Obejective: Hide node "edit" tab for certain content types and roles in D7.

3
  • Why you want to hide editing without disallowing it? What sense it is supposed to make? Everyone knows to just add /edit at the of of the address anyway, right?
    – Mołot
    Commented Oct 31, 2014 at 12:18
  • I am aware of that, objective of this is not to restrict access but for themeing reasons. As already stated, An edit button is going to be created manually on node pages.
    – ahmadk
    Commented Oct 31, 2014 at 16:51
  • theme_menu_local_tasks Commented Oct 31, 2014 at 17:14

1 Answer 1

2

I had the same issue and could solve that using template_preprocess_page(&$variables).

Open template.php under [yoursite]/themes/[your-theme]

For example if you're using bartik theme and want to hide tabs (View and Edit) for Article content type for Editor role.

Paste the following code in your template.php

/**
 * Remove tabs on article content type
 */
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]);
                    }
                }
            }
        }
    }
}

Clear your site cache and test.

This removes the View and Edit tabs on Article node pages for editor roles users. But they can access those pages via URL.

Hope this helps.

2
  • Thanks for the snippet, but how can it be used for multiple content types like article & blog, and roles like editor & anonymous?
    – ahmadk
    Commented Nov 13, 2014 at 9:28
  • @ahmadk you can add conditions like $variables['node']->type == 'your_content_type' please see code snippet. and if you feel my answer helpful, approve this.. so that it would be removed from unanswered list and might be helpful for future readers Commented Nov 13, 2014 at 9:33

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.