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.

  • 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 Oct 31 '14 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 Oct 31 '14 at 16:51
  • theme_menu_local_tasks – J. Reynolds Oct 31 '14 at 17:14
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.

| improve this answer | |
  • Thanks for the snippet, but how can it be used for multiple content types like article & blog, and roles like editor & anonymous? – ahmadk Nov 13 '14 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 – Krishna Mohan Nov 13 '14 at 9:33

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.