I am trying to use hook_menu_alter to change the title and weight of the node edit tab, but only for the group node type. However, the following (which is inspired by http://www.nicklewis.org/node/999) doesn't work.

$node = menu_get_object();
if (og_is_group_type($node->type) == 'TRUE') {
  $items['node/%node/edit']['title'] = t('Group settings');
  $items['node/%node/edit']['weight'] = 6;
}

It seems that the $node variable gets set the first time and simply doesn't change after that. It can be seen from this code:

$node = menu_get_object();
$items['node/%node/edit']['title'] = $node->type;

If the first page I watch after clearing the cache is of type 'page', then page is output on every node type. How can that be? Any idea is appreciated.

Marcus

Comments

awolfey’s picture

We can't see your complete code, but you need to make sure the hook_menu_alter is calling another function to get the $node, rather than loading the node in the hook, where it will only be done when the menu is rebuilt.

For example, here I'm changing the title of the "View" tab when the node type is "vehicle".

<?php

function custom_menu_alter(&$items) {
  $items['node/%node/view']['title'] = custom_node_view_tab();
  $items['node/%node']['title'] = custom_node_view_tab();
}

function custom_node_view_tab() {
  $node = menu_get_object();
  if ($node->type == 'vehicle') {
    return 'Stats';
  }
  return 'View';
}
?>
DieterWeb’s picture

this solution won't work, in fact it doesn't change anything if you use another function or set the menu items in the hook. if you think about it this makes sense, because if the hook is cached, it won't be called again, therefore your extra function won't get called too. i've tried the following to verify it (it tries to disable a menu item of the bbb module only if the node type is "classroom"):

function foo_menu_alter(&$items)
{
    $items['node/%node/meeting/attend'] = foo_meeting_attend_button();
}
function foo_meeting_attend_button()
{
  $node = menu_get_object();
  if ($node->type == 'classroom') {
  	return false;
  }
  return array
  (
    'title' => 'Attend Meeting',
    'page callback' => 'bbb_join',
    'page arguments' => array(1, 3),
    'access callback' => 'bbb_access_attendee',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );
}

after clearing all caches, everything works as expected and the menu item is disabled if i go to a classroom node page. now theres two ways to check if its really dynamic or if it is coming from cache. first, just access another page with these menu items with a different node type, or change the node type "classroom" to something else in the code. now, without clearing the cache, access the page, and you will see, that the data comes from cache. so even the node type is not classroom, the menu item is disabled, or if you change the node type name to something else, and refresh the classroom node page, it stays disabled.

i am still looking for a solution for this, if someone has any idea, it will be greatly appreciated.

awolfey’s picture

Yes, of course it doesn't work! I didn't test it thoroughly. Thanks for the catch. I'll reply again when I have a soution.

e_oliver’s picture

Yeah actually you have to access the title callback property, not the title. Then you can do something like this:

function somemodule_menu_alter(&$items) {     
$items['node/%node/edit']['title callback'] = 'override_edit_title';
$items['node/%node/edit']['title arguments'] = array(1);
}

//Change "Edit" tabname to "Modify" for pages of your_content_type
function override_edit_title($node) {
  if ($node->type == 'your_content_type') {
    return t('Modify');
  }
//else return the default name
  else {
    return t('Edit');
  }
}

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.