hello everybody

I want to add a link in the primary links to my own module

<?php

// $Id$

/**
* @file
* Module for shopping basket
*/

function StevenContact_menu(){
$items['admin/settings/StevenContactAdmin'] = array(
...
);

$items['admin/settings/StevenContactClient'] = array(
'title' => 'Steven contact form',
'page callback' => 'kensoContact_client_form',
'access arguments' => array('Access Admin KensoContact'),
'description' => t('The contact form for clients'),
'access callback' => 'user_access',
'type' => MENU_NORMAL_ITEM,
);

return $items;
}

...

the second item has to be linked in primary links

can somebody help me?

PS: I know you can do this in menus-> primary links -> add item, but i need to do this in code...

Comments

nirbhasa’s picture

You could override it in your theme, here we call the theme 'yourtheme'


function yourtheme_menu_links($links) {
  if (!count($links)) {
    return '';
  }
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  $output = "<ul class=\"links-$level\">\n";
  foreach ($links as $index => $link) {
    $output .= '<li';
    if (stristr($index, 'active')) {
      $output .= ' class="active"';
    }
    $output .= '>'.l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'])."</li>\n";
  }
  //Here is where you insert your own link
  $output .= "<li>". l('Steven contact form', 'admin/settings/StevenContactClient') ."</li>\n";
  
  $output .= '</ul>';

  return $output;
}

It is quite possible that this function already exists in your template.php file, so all you have do so is add that line inserting the link (btw it might be themed differently than the default bulleted list above, but im sure youll figure that out)

This is probably not the cleanest way, but im not sure how do do it inside a module.

(Edit: apologies: this is a drupal 5 way of doing things, ill investigate further for D6)

nirbhasa’s picture

Ah, here we go

There is a way to do it inside a module after all

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

menu_link_save('link_title' => 'Steven contact form', 'link_path' => 'admin/settings/StevenContactClient', 'menu_name' => 'primary_links')

There are other keys you can add, but that's the bones of what you're looking for, I think.

stevieke’s picture

thanx a lot, but... it doesn't work

Do i have to set this code in .install or . module?
When i'm gonna look in my database, there is a new record, so it is submitted... but my link doesn't appear under primary links...
also when i'm leave menu_name away the link doesn't appear... what am I doing wrong

function stevenContact_install(){

$link = array();
$link['link_title'] = 'Contact';
$link['link_path'] = 'admin/settings/stevenContactClient';
$link['menu_name'] = 'primary_links';
$link['mlid '] = NULL;
menu_link_save($link);

drupal_install_schema('stevenContact');
}

nirbhasa’s picture

It needs to be set in .module. Also clearing the menu cache afterwards is probably a good idea

stevieke’s picture

thanks a lot,it works

fatfish’s picture

Can I use in the hook_menu? I do not want to fire it on every page load...

..:| Tomer Fish |:..
fatFish - Lean Mean Coding Machine

csg’s picture

I have the same question. My guess is hook_enable(), but a confirmation would be nice.

csg’s picture

I tried hook_enable() and it works.

Don't forget to use hook_disable() to remove the link when your module is disabled!