I would like to learn a bit of drupal development.

Now I came across hook_menu. Is it possible to add an item to the primary links with this hook?

Thanks,

Eugene

Comments

styro’s picture

hook_menu adds a menu item to Drupal with a default path/location.

The location of that menu item may (depending on the menu type you give it in the module) be able to be changed by the site admin on the menu admin pages. Also the site admin chooses which menu is the primary links menu.

So you can create a module that creates a menu item, and that menu item can be put in the menu that gets used for the primary links.

--
Anton

Eugene Dubois’s picture

Thank you for the answer Anton.

What I would like to achieve on my website is, that members with a certain role will get an extra tab in the primary links menu at the top of my page. Is this the best way to do that, or is there an other option?

Eugene

styro’s picture

As far as I know there isn't a contrib module for menu item access control (but I've lost touch with the sheer volume of Drupal modules these days).

If that is the case then yes, creating the menu item yourself in a module would be the best way to control access. hook_menu() allows to to specify a function to be an "access callback" for that menu item:

http://api.drupal.org/api/function/hook_menu/6

--
Anton

bidwella’s picture

I Just got a primary link added using 'menu_name' in my menu item (i'm using drupal 6):

'menu_name' => 'primary-links',

Then, apply access control on the tab by specifying:

'access arguments' => array('access mymodule content'),

So, something like the following might help you:

function mymodule_menu()
{
        $items = array();

        $items['mymodule'] = array(
                'title' => t('My Module'),
                'page callback' => 'mymodule_homepage',
                'access arguments' => array('access mymodule content'),
                'type' => MENU_NORMAL_ITEM,
                'menu_name' => 'primary-links',
                'weight' => 0,
        );

        return $items;
}

function mymodule_perm()
{
        return array(
                'access mymodule content',
        );
}
greendwarf’s picture

Thank you! I was able to get my modules to add to the primary links and only show for the correct permissions after reading this.

nlambert’s picture

Hi,

This seems easy enough, but it's not working !

I've created a custom module and implemented hook_menu.

I can see the new menu links (xxxxx and zzzzz) in the menu_links and menu_router sql tables. I can see them along with the links I created with admin/build/menus. Unfortunately the Primary Links Block only produces the links from admin/build/menus.

I've cleared the cache, I've rebuild the menus, nothing... :-/

edit : oh, and I've also tried with links that actually exist (node/1, node/2) and tried prefixing primary-links like so : menu-primary-links (saw this in other forums).

edit 2 : I'm also noticing that when I use an existing url (node/1, node/2) the menu_name is forced to "navigation"

Any thoughts?
Cheers

function my_module_menu() {
	$items = array();
	$items['zzzzz'] = array(
		'title' => t('ZZZZZ'),
		'menu_name' => 	'primary-links',
		'type' => MENU_NORMAL_ITEM,
	);
	$items['xxxxx'] = array(
		'title' => t('XXXXX'),
		'menu_name' => 	'primary-links',
		'type' => MENU_NORMAL_ITEM,
	);
	return $items;
}
nlambert’s picture

I needed a valid callback. I was expecting the link to appear regardless.