Skip to content

Instantly share code, notes, and snippets.

@modcab
Created June 2, 2016 10:21
Show Gist options
  • Save modcab/a12a11a70cf0f3eec825e80a1c4808f9 to your computer and use it in GitHub Desktop.
Save modcab/a12a11a70cf0f3eec825e80a1c4808f9 to your computer and use it in GitHub Desktop.
Drupal 7 - Programmatically create children menu items in different languages.
function sega_hero_update_7001() {
// Load all 'Mobile' menu items in the different languages.
$parent_menu_items = db_select('menu_links', 'l')
->fields('l')
->condition('link_title', 'Mobile')
->condition('menu_name', 'main-menu')
->execute()
-> fetchAllAssoc('language');
// Children menu items we're going to create.
$new_menu_items = array(
array(
'title' => 'Games',
'path' => 'games?field_tags_tid=129'
),
array(
'title' => 'Blog',
'path' => 'http://mobile.sega.com'
),
);
// Iterating over parent items. We'll use their language and mlid.
foreach ($parent_menu_items as $lang => $parent_menu_item) {
// Iterating over the info for creating the new childrem items.
foreach ($new_menu_items as $info) {
$form_state = array(
'values' => array(
'link_path' => $info['path'],
'link_title' => $info['title'],
'menu_name' => 'main-menu',
'plid' => $parent_menu_item['mlid'],
'language' => $lang,
),
);
module_load_include('inc', 'menu', 'menu.admin');
// Validate and transform the item, so it conforms with Drupal standards.
menu_edit_item_validate(array(), $form_state);
// Save the item to database.
menu_link_save($form_state['values']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment