Sorry for this newbie question.

I just got this code below for adding a new tab on view/edit node. But I dont know where to put this code? Do I need to create a new file for this? Or is there an existing file I need to edit.

$items[] = array(
'path' => 'node/' . arg(1) . '/yourpath',
'title' => t('Your title'),
'type' => MENU_LOCAL_TASK,
'callback' => 'your_callback',
);

Thanks in advance.

Comments

rschwab’s picture

You need to make a module. That code will go in your mymodule.module file. Here is a link that will help get you started:

http://drupal.org/node/231276

- Ryan

zeed45’s picture

Thank you for the reply, Ryan. But is there a way I can just modify the node module to add a new tab on edit/view node? Or do I really need to create a separate module?

rschwab’s picture

You could, but its not a good habit to get into. The reason being that drupal is updated relatively often, and with each security update or new feature you would have to go back into the node.module and make your changes again. If you create it in a separate module then when updates come out your fix is still in place.

It might seem like a lot to make a module, but if you just want to make a few changes here is a quick guide:

1. Make a folder in your sites/all/modules folder and name it exactly what your module will be called. We'll call our example mymodule.
2. make a mymodule.info file inside the folder. Its contents should look like:

; $Id$
name = My module name
description = Various customizations for my drupal theme
core = 6.x

3. Make a mymodule.module file. Its contents should look like:

<?php
//$Id$

/**
* @file
* Various customizations for my drupal theme
*/

/**
*  Implementation of hook_menu_alter
*/
function mymodule_menu_alter(&$items) {
$items[] = array(
'path' => 'node/' . arg(1) . '/yourpath',
'title' => t('Your title'),
'type' => MENU_LOCAL_TASK,
'callback' => 'your_callback',
);
}
?>

In your code don't include the last "?>". For drupal to function right you need to leave the php tag open.
Voila. Now upload it, go into Site building -> modules and enable it.

- Ryan

zeed45’s picture

Thanks, Ryan. I was able to activate the module but I cannot see the new tab. I might have missed something. Below is my current code. I want add a "Food" tab on this path "http://localhost/test/?q=node/9/meals". Is my path value correct? Once I modify mymodule.module file, will the change take effect right away? Or do I need to re-activate the module.

<?php
//$Id$

/**
* @file
* Various customizations for my drupal theme
*/

/**
* Implementation of hook_menu_alter
*/
function mymodule_menu_alter(&$items) {
$items[] = array(
'path' => 'node/' . arg(1) . '/meals',
'title' => t('Food'),
'type' => MENU_LOCAL_TASK,
'callback' => 'your_callback',
);
}

rschwab’s picture

Try this:

<?php
//$Id$

/**
* @file
* Various customizations for my drupal theme
*/

/**
* Implementation of hook_menu
*/
function mymodule_menu() {

$items['node/%node/meals'] = array(
  'title' => t('Food'),
  'type' => MENU_LOCAL_TASK,
  'callback' => 'your_callback',
);

return $items;
}

Because the menu is cached, you'll want to clear your cache and rebuild your menu when making changes to hook_menu or hook_menu_alter... but in most situations you just save your module file and refresh the page.

- Ryan

zeed45’s picture

its kinda weird though. I rename '..%/meals' to '..%meal' and added 'access arguments' => array(1). And it worked. But it was visible to all nodes. Is there a way to show it only under this link http://localhost/test/?q=node/9/meals ?

function mymodule_menu() {
$items['node/%/meal'] = array(
'title' => t('Food'),
'page callback' => 'mycallback',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
return $items;
}

thanks.

rschwab’s picture

You mean only show it on node 9? If so change your functions first line from

$items['node/%/meal'] = array(

to

$items['node/9/meal'] = array(

- Ryan

zeed45’s picture

tried both node/9/meal and node/9/meals , but it has no effect still showing up on all nodes.

rschwab’s picture

did you flush out the cache? Menu only gets rebuilt when you change modules or flush it.