-1

Unlike the author of https://stackoverflow.com/questions/3530756/how-to-hide-edit-view-tabs I'd like to totally replace node tabs with what users see on their user profile pages.

Users on this website won't be able to edit their nodes and admins can just type node/%/edit or be provided with a link on special block, so node tabs are useless for this project, but user tabs are essential as there are no community nodes, users are locked to their accounts and need to see all their nodes under user accounts.

I tried to use the hook_menu_local_tasks_alter, it correctly adds new tab items, but I don't know how to get rid of the existing ones. I tried to hide them with Tabtamer module, but unfortunately it gives:

Notice: Undefined index: count in menu_primary_local_tasks() (line 2240 of ../includes/menu.inc).

Thus the existing items must exists, so instead of adding new individual items with hook_menu_local_tasks_alter, I would like to just check if arg(0) == 'node' then replace the node tabs totally with what users usually see on their profile pages.

How could I totally replace node tabs with user tabs on node pages?

  • First of all, before anyone even attempts to answer this question, we need to know your Drupal version as the answer will differ. – No Sssweat Jan 9 '17 at 1:03
  • 1
    I'm not clear which tabs you want on node pages. Do you mean user view and user edit tabs? – Jaypan Jan 9 '17 at 1:38
  • @no-sssweet, sorry I've just edited and indicated Drupal 7 in the title. – Nick Jan 9 '17 at 1:48
  • @Jaypan, yes I mean view and edit tabs, but not for nodes. – Nick Jan 9 '17 at 1:48
2

For Drupal 7, you should be able to do the following:

1) Implement hook_menu(), and define new tabs as local tasks on the node path.

2) Set the callback for these tabs to redirect to the relevant user pages

3) Implement hook_menu_alter() to remove the tabs from the node pages.

Step 1: Implement hook_menu(), and define new tabs as local tasks on the node path.

function hook_menu()
{
  $menu['node/%node/user_view'] = array
  (
    'title' => 'View',
    'page callback' => 'node_user_redirect',
    'page arguments' => array('view'),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_LOCAL_TASK,
  );

  $menu['node/%node/user_edit'] = array
  (
    'title' => 'Edit',
    'page callback' => 'node_user_redirect',
    'page arguments' => array('edit'),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_LOCAL_TASK,
  );

  return $menu;
}

Step 2: Set the callback for these tabs to redirect to the relevant user pages

function node_user_redirect($type)
{
  global $user;

  drupal_goto('user/' . $user->uid . '/' . $type);
}

Step 3: Implement hook_menu_alter() to remove the tabs from the node pages.

function hook_menu_alter(&$menu)
{
  // Remove the view tab altogether
  unset($menu['node/%node/view']);
  // Set the edit page to not present a tab on node pages
  $menu['node/%node/edit']['#type'] = MENU_CALLBACK;
}
  • Jaypan, thank you very much, but apparently we were submitting answers at the time. I have already found the right solution to my case, but I'll accept your answers as the correct one too. – Nick Jan 9 '17 at 2:10
0

Sometimes so obvious solutions do not appear at once to average minds like mine ;) I just had to run check if (arg(0) == 'admin' || arg(0) != 'node') { return; } in my hook and everything works just as expected - I can add user tabs to nodes tabs.

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.