I'm trying to create special menu items via menu_link_save in a hook_update_n within an install profile. Upon initial site install, the menu_link_save does not create the special menu item. However, if I run the hook_update_n AFTER the initial site install, the menu_link_save works correctly.
I've confirmed that special menu items is being enabled prior to my hook_update_n running with a drush si -d
Any suggestions? My code is below.
/**
* Implements hook_update_N().
* Generate menu links
*/
function theme_config_feature_update_10001(&$sandbox) {
cache_clear_all();
//generate the menu links array
//for the header menu
$menu_links = array();
$menu_links['account'] = array(
'link_path' => 'user',
'link_title' => t('Login'),
'weight' => 0,
);
$menu_links['telephone'] = array(
'link_path' => '<nolink>',
'link_title' => t( '1-800-123-4567'),
'weight' => 2,
'options' => array(
'attributes' => array(
'class' => array('phone-number', 'nolink')),
'html' => TRUE,
),
);
//run menu_link_save on all of the items.
foreach ($menu_links as $menu_link) {
$menu_link['menu_name'] = 'menu-header';
$menu_link['module'] = 'theme_config_feature';
$menu_link['options']['attributes']['class'][] = str_replace(' ', '-', $menu_link['link_title']);
menu_link_save ($menu_link);
}
menu_cache_clear_all();
return t('Header menu links have been generated');
}
Comments
Comment #1
emarchak commentedFor more details, here's the relevant output from drush si -d
Comment #2
richsky commentedTrue, I can't create special menu items programmatically either.
Comment #3
gagarine commentedlinked to #1355088: Features compatibility (path need to be unique)
Comment #4
stefanschramm commentedAs a workaround try calling menu_cache_clear_all(); before and after your menu_link_save() calls.
It seems as if the menu entries are inserted into the menu_links table, but are getting removed later - probably while clearing the "cache" (there will be a gap in the mlids). Maybe this is caused by some other module missing to call menu_cache_clear_all() as stated in the menu_link_save() documentation. However, it's strange that "caching"-mechanisms will cause the actual data to be modified...
Comment #4.0
stefanschramm commentedSyntax error