Using MENU_LOCAL_TASK tabs to link around the site

I'm sure many of you have been asked this by your clients:
"Can we add a link up the top there to create a new (listing/page/article)?"

This isn't difficult, I'm merely sharing with you a way to make this sort of UI tweak much simpler.

Normally, I would have created a new menu item in hook_menu, and in the custom page callback do a drupal_goto.

<?php
/**
 * Implement hook_menu
 */
function mymodule_menu() {
  $menu['user/%user/add_listing'] = array(
    'title' => 'Add a Listing',
    'description' => 'Click here to create a new listing',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'mymodule_redirect_add_listing',
    'access callback' => 'user_is_logged_in',
  );
  return $menu;
}
 
/**
 * Page callback which redirects to the add listing page
 */
function mymodule_redirect_add_listing() {
  // this takes them to the listing add page and then redirects back to the user page they came from
  drupal_goto('node/add/listing', 'destination=user');
}
?>

While the above example gets the job done, it can be done in a single step rather than 2:

<?php
/**
 * Implement hook_menu
 */
function mymodule_menu() {
  $menu['user/%user/add_listing'] = array(
    'title' => 'Add a Listing',
    'description' => 'Click here to create a new listing',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_goto',
    'page arguments' => array('node/add/listing', 'destination=user'),
    'access callback' => 'user_is_logged_in',
  );
  return $menu;
}
?>

By changing the page callback to drupal_goto and adding the destination as the page argument, there is no need for a page callback which does exactly the same thing.

So for all you neat freaks out there, this can save you up to 8 lines of custom code per redirect!!

What is the path of

What is the path of "node/add/listing" ? a drupal internal path ? or it is a path point to somewhere like a view of node list

This particular client had a

This particular client had a content type called 'listing'. In a plain Drupal installation, node/add/page will take you to a page node form. In this particular site node/add/listing took them to a listing node form.

However, this technique can link to wherever you like, both internal drupal links and external links will work.

Never knew that, much cleaner

Never knew that, much cleaner way to implement such menu items, thanks!

Even better yet: Drupal

Even better yet: Drupal 7

http://api.drupal.org/api/function/hook_menu_local_tasks_alter/7

I copied the code in a

I copied the code in a mymenu.module and enabled the modul. But the link doesn't appear. I tried the first and the second code but nothing happens.
What have i done wrong?

hi erzone, you will need to

hi erzone, you will need to rename the function to match your module name - in your case the function name should be mymenu_menu()

If, after renaming the function and enabling your module the menu still doesn't appear, try going to admin > settings > performance and refreshing your site's cache.

Hi, i tried the way you said

Hi, i tried the way you said but it doesn't still work.
I also refreshed the site cache. I can see the modul in the modul page, but after activation the link doesn't show up.
Does the code work for anybody?

Hi erezone, maybe you should

Hi erezone, maybe you should jump into the IRC support rooms (#drupal-support) on irc.freenode.net - there will be people in there who can help you debug your issues in a timely manner.

ok, so I will do. Thank's for

ok, so I will do.
Thank's for your support.

Does anyone know the path to

Does anyone know the path to link the blog of the profile being viewsed

blog/$author->uid ?

I think it's

I think it's user/$author->uid/blog

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal5>, <drupal6>, <javascript>, <php>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.