I'm trying to programmatically move and enable a menu link:

  $contact = db_fetch_array(db_query("SELECT 'mlid', 'link_path', 'link_title' FROM {menu_links} WHERE link_path = 'contact'"));
  $contact['weight'] = 50;
  $contact['menu_name'] = 'primary-links';
  $contact['hidden'] = 0;
  menu_link_save($contact);

My code doesn't seem to do anything at all (but no errors). Any idea why? Is there some other way I should be doing this?

Comments

mooffie’s picture

During menu rebuild, which happens from time to time, modules' links are written anew to that table. So your changes aren't going to survive.

Doing $contact['customized'] = 1 might solve your problem: Drupal preserves links that were customized.

(Another thing to try: immediately after doing menu_link_save(), read from that table and verify that the old link was indeed overwritten.)

seanr’s picture

The values were written (already checked that), but yes, the menu_rebuild() function overwrites them. Where does it get the data from?

seanr’s picture

This code just hosed my site - I now get a 404 on everything including the homepage:

$contact = db_fetch_array(db_query("SELECT 'mlid', 'link_path', 'link_title' FROM {menu_links} WHERE link_path = 'contact'"));
$contact['weight'] = 50;
$contact['menu_name'] = 'primary-links';
$contact['hidden'] = 0;
$contact['customized'] = 1;
menu_link_save($contact);
dsm(menu_link_load($contact['mlid']));
menu_rebuild();
dsm(menu_link_load($contact['mlid']));
seanr’s picture

This code just hosed my site - I now get a 404 on everything including the homepage:

$contact = db_fetch_array(db_query("SELECT 'mlid', 'link_path', 'link_title' FROM {menu_links} WHERE link_path = 'contact'"));
$contact['weight'] = 50;
$contact['menu_name'] = 'primary-links';
$contact['hidden'] = 0;
$contact['customized'] = 1;
menu_link_save($contact);
dsm(menu_link_load($contact['mlid']));
menu_rebuild();
dsm(menu_link_load($contact['mlid']));
seanr’s picture

Still no luck with this:

  $item= menu_link_load(db_result(db_query("SELECT 'mlid' FROM {menu_links} WHERE link_path = 'contact'")));
  $item['original_item'] = $item;
  $item['customized'] = 1;
  $item['weight'] = 50;
  $item['menu_name'] = 'primary-links';
  $item['hidden'] = 0;
  $item['options']['attributes']['title'] = $item['description'];
  list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);
  if (!menu_link_save($item)) {
    drupal_set_message(t('There was an error saving the menu link.'), 'error');
  }

Doesn't bork the site at least, but it also doesn't do a damned thing. :-(

seanr’s picture

Got it with this:

  $contact = menu_link_load(db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'contact'")));
  $contact = array_merge((array)$contact, array('original_item' => $contact, 'customized' => 1, 'weight' => 49, 'hidden' => 0, 'menu_name' => 'primary-links', 'plid' => '0',));
  menu_link_save($contact);
chx’s picture

This forum post talks about menu links. The other thing hook_menu generates are router items.

--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.