Sometime your wan’t to display menus and tabs in drupal as per the nodetype. For example, you want “Images” tab in case of nodetype “Hotel” but not want this when story or page nodetype is viewed. In drupal 6.x, the menu system is changed a lot.
Drupal 6.x Code:
<?php $items['node/%hotel_node/images'] = array( ‘title’ => ‘Images’, ‘type’ => MENU_LOCAL_TASK, ‘weight’ => -10, ‘page callback’ => ’show_tab_page’, ‘page arguments’ => array(1,’images’), ‘access callback’ => TRUE, ‘access arguments’ => array(1), ); function hotel_node_load($nid) { if (is_numeric($nid)) { $node = node_load($nid); if ($node->type == ‘hotel’) { return $node; } } return FALSE; } ?>
Here, when the menu system is build its call “hotel_node_load()” function with “$nid” as function parameter. We will check the type of node and if the current node type is “hotel” we will return currently loaded node. Otherwise we will return FALSE. This way only node having type “hotel” will display “Images” tab.
Drupal 5.x code:
In drupal 5.x we can do same thing like this.
<?php if (arg(0) == ‘node’ && is_numeric(arg(1))) { $node = node_load(arg(1)); if ($node->type == ‘hotel’) { $items[] = array( ’path’ => ‘node/’. arg(1), ‘title’ => t(’Images’), ‘callback’ => ’show_tab_page’, ‘callback arguments’ => array($node), ‘access’ => node_access(’view’, $node), ‘type’ => MENU_CALLBACK); } } ?>
For more information click here
#1 by Jim - April 9th, 2009 at 18:22
Added to DrupalSightings.com
#2 by ningku - May 26th, 2009 at 11:10
it doesnt work to me.
I try the following code:
[code]function forum_menu_menu() {
$items['node/%node/forum']= array(
'title' => 'Forums',
'page callback' => 'forum_page',
'page arguments' => array(1),
'access callback' => 'get_page_type',
'access arguments' => array(1),
'weight' => 10,
'file' => 'forum.pages.inc',
'file path' => drupal_get_path('module', 'forum'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function get_page_type($nid) {
if (is_numeric($nid)){
$node = node_load($nid);
if($node->type == 'page'){
return $node;
}
}
return FALSE;
}
[/code]
would you tell me where the error is??
thanks
#3 by admin - May 26th, 2009 at 11:57
@ningku
Actually you are not using the wildcard loader function properly.
You have used ‘%node’ in your path ‘node/%node/forum’. So, the wildcard loader function name should be ‘node_load()’.
In your case, your path should be ”node/%get_page_type/forum” and your wildcard loader function should be
function get_page_type_load($nid) {
}
Drupal itself add ‘_load’ to wildcard function name when call it.
One more thing, the ‘access callback’ should be the array of permissions. So, roles having given permission can only access this path.
Hope this will helpful to your.
-Anil
#4 by Ningku - May 27th, 2009 at 15:15
Firstly I would like to thank you for the quick respond, I have tried to add wildcard loader as you mentioned, but I still get the error, the new tab [Forum] still appears on other node content type.
And here is the new code:
function forum_menu_menu() {
$items['node/%get_page_type/forum']= array(
'title' => 'Forums',
'page callback' => 'forum_page',
'access callback' => TRUE,
'access arguments' => array('access content'),
'weight' => 10,
'file' => 'forum.pages.inc',
'file path' => drupal_get_path('module', 'forum'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function get_page_type_load($nid) {
if ($nid) {
$node = node_load($nid);
if ($node->type == 'page') {
return $node;
}
}
}
Also I tried to remove $nid on node_load(), but it gives me error message.
I hope you don’t mind helping me more, I am kinda newbie in Drupal and I feel so stupid cuz I have been working on this menu tabs for 3 days and I don’t solve anything..
Regards,
Ning
#5 by admin - May 27th, 2009 at 16:27
@Ningku
Return “FALSE” at the end of your function “get_page_type_load()”. So, if node type don’t match then function return FALSE. It is important.
function get_page_type_load($nid) {
if ($nid) {
$node = node_load($nid);
if ($node->type == ‘page’) {
return $node;
}
}
return FALSE;
}
One more thing, what’s your module name because the “hook_menu” should be “module_name_menu” and your hook_menu is “forum_menu_menu”.
Let me know if this fix the issue.
#6 by Ningku - May 28th, 2009 at 09:08
Hi,
My module name is forum_menu.module, thats why I made function forum_menu_menu(), but now I have changed it become fmenu.module and I also changed the function because I thought that the word of “menu” on the module would affect the hook menu.
I tried to add “return false;” on the function but the menu tab for forum disappear on all node type. also I tried to type the link: http://localhost/drupal/?q=node/7/forum to make sure if forum page appears, but it says “page not found” .
And I found the instruction here: http://drupal.org/node/227531, I did the same thing and it did not fix the issue.
I can not find where the bug is.
Regards,
Ning
#7 by Ningku - May 28th, 2009 at 10:18
Btw, I just check the edit page on node/%node/edit it also shows me error message:
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘_node_form’ was given in D:\xampplite\htdocs\drupal\includes\form.inc on line 366
#8 by admin - May 28th, 2009 at 10:39
@ Ningku
Hi Ning,
As per your “get_page_type_load()” function the “Forums” tab will only come on the “page” type node. So, what is they type of node whose ID is ’7′. If ’7′ is node of type “page” then only “Forums” tab come.
I didn’t found any other problem in your code. The only reason that “Forums” tab is disappears is that your “get_page_type_load()” function always return FALSE. “if ($node->type == ‘page’) ” condition always return false.
Try var_dump() to check that that program is coming under this condition.
if ($node->type == ‘page’) {
var_dump($node);
die();
return $node;
}
-Anil
#9 by Ningku - May 28th, 2009 at 11:54
@ admin
Awww.. I have been working offline and it keeps getting error, but just now I tried to put the code on my online drupal site, it works! I should have tried it since this morning, sorry for this.
Thanks Anil, you are so helpful
Regards,
Ning
#10 by admin - May 28th, 2009 at 11:58
@ Ningku
Great!
- Anil
#11 by Sanjeet - April 12th, 2011 at 13:47
type == ‘hotel’) {
$items[] = array(
’path’ => ‘node/’. arg(1),
‘title’ => t(’Images’),
‘callback’ => ’show_tab_page’,
‘callback arguments’ => array($node),
‘access’ => node_access(’view’, $node),
‘type’ => MENU_CALLBACK);
}
}
?>