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

emarchak’s picture

For more details, here's the relevant output from drush si -d

WD system: menu module enabled. [12.73 sec, 44.01 MB]
WD system: special_menu_items module installed. [12.82 sec, 44.17 MB]   
WD system: special_menu_items module enabled. [12.82 sec, 44.17 MB]    
WD system: theme_config_feature module installed. [12.92 sec, 44.31 MB]   
WD actions: Action 'Publish comment' added. [13.33 sec, 44.77 MB]    
WD actions: Action 'Unpublish comment' added. [13.33 sec, 44.77 MB]    
WD actions: Action 'Save comment' added. [13.33 sec, 44.77 MB]    
WD actions: Action 'Publish content' added. [13.33 sec, 44.77 MB]    
WD actions: Action 'Unpublish content' added. [13.34 sec, 44.77 MB]    
WD actions: Action 'Make content sticky' added. [13.34 sec, 44.77 MB]   
WD actions: Action 'Make content unsticky' added. [13.34 sec, 44.78 MB]   
WD actions: Action 'Promote content to front page' added. [13.37 sec, 44.78 MB]  
WD actions: Action 'Remove content from front page' added. [13.37 sec, 44.78 MB]  
WD actions: Action 'Save content' added. [13.38 sec, 44.78 MB]    
WD actions: Action 'Ban IP address of current user' added. [13.38 sec, 44.78 MB]  
WD actions: Action 'Block current user' added. [13.38 sec, 44.78 MB]   
WD features: Rebuilding theme_config_feature / menu_custom. [13.56 sec, 48.96 MB]  
WD features: Rebuild completed for theme_config_feature / menu_custom. [13.57 sec, 48.98 MB]
richsky’s picture

True, I can't create special menu items programmatically either.

gagarine’s picture

Version: 7.x-1.0 » 7.x-2.x-dev
stefanschramm’s picture

As 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...

stefanschramm’s picture

Issue summary: View changes

Syntax error