Skip to content

Instantly share code, notes, and snippets.

@Unifex
Last active May 31, 2016 22:38
Show Gist options
  • Save Unifex/7db225da0cb406b3d9b00c70b44b5103 to your computer and use it in GitHub Desktop.
Save Unifex/7db225da0cb406b3d9b00c70b44b5103 to your computer and use it in GitHub Desktop.
Drupal 7 has a reasonably messed up way of handling menus and I was unable to find a simple way of saying "starting here give me 2 levels of menu." This does that for you. It returns an array of menu items. If you've set depth to anything greater than 0 it will append these and an array to the `children` key. No authentication or validation is p…
/**
* Helper funtion to get an array of menu items and children.
*
* This returns an array of menu items with child menu items added to the
* 'children' key.
*
* @param int $mlid
* The menu link ID we are looking at.
* @param int $depth
* How deep do you want to go?
*/
function _menu_array_with_children($mlid, $depth = 0) {
$ret = array();
// Do we have children?
$query = db_select('menu_links', 'ml')
->fields('ml')
->condition('plid', $mlid)
->orderBy('weight')
->execute();
while ($child = $query->fetchAssoc()) {
if ($depth > 0 && $child['has_children']) {
// We want more.
$child['children'] = ec_landing_page_nested_children($child['mlid'], $depth - 1);
}
$ret[] = $child;
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment