Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Created July 12, 2017 01:43
Show Gist options
  • Save doublejosh/a7145993a0e30012ddf8fb1b3c3104d9 to your computer and use it in GitHub Desktop.
Save doublejosh/a7145993a0e30012ddf8fb1b3c3104d9 to your computer and use it in GitHub Desktop.
Add Drupal menu items with hierarchy (not working right)
/**
* Set the parent of menu items via name.
*
* @param string $menu_name
* Machine name of the menu.
* @param string $item_name
* Title of the menu link to use as the new parent.
* @param array $children
* List of menu item (mlids) to nest under parent.
*/
function _tableau_updates_set_menu_parent_by_name($menu_name, $item_name, $children) {
// Find parent menu item.
$menu_list = menu_load_links($menu_name);
foreach ($menu_list as $menu_item) {
if ($menu_item['link_title'] === $item_name) {
// Update children items.
foreach ($children as $child_item) {
// Save with a new parent.
$child_menu_item = menu_link_load($child_item);
$child_menu_item['plid'] = $menu_item['mlid'];
menu_link_save($child_menu_item, $child_menu_item);
}
}
break;
}
}
/**
* Add industry sector menu items to Solutions expand-o-nav.
*/
function tableau_stories_update_7204() {
// Intended hierarchy.
$sectors = array(
'Communications, Media & Technology' => array(52890, 52888, 52885, 52889),
'Energy & Resources' => array(52816),
'Financial Services' => array(52812, 52882, 52883),
'Healthcare & Life Sciences' => array(52881, 52886),
'Manufacturing' => array(52884),
'Public Sector' => array(52811, 52814, 52815, 52880, 52817),
'Retail & Consumer Goods' => array(52813, 52887),
'Services' => array(),
'Travel & Transportation' => array(),
);
// Create new menu items.
$menu_properties = array(
'plid' => 52937, // Industries.
'menu_name' => 'menu-stories-topics',
'link_path' => '<nolink>',
'module' => 'menu',
'expanded' => 1,
'customized' => 1,
'language' => 'und',
);
foreach ($sectors as $sector_name => $sector_items) {
$menu_item = array_merge($menu_properties, array('link_title' => $sector_name));
print_r($menu_item);
menu_link_save($menu_item, array(), array(52937));
//_tableau_updates_set_menu_parent_by_name('menu-stories-topics', $sector_name, $sector_items);
}
menu_cache_clear_all();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment