I have just finished posting a function that gets child menu items given a node's path. You can check it here: http://softkube.com/blog/getting-child-menu-items-drupal-menu-tree
I'm including the link to future proof the answer in case the post gets updated and I'll also copy/paste the full code at the end.
In your case, you can simply run something like this in your theme to list all child menu items. Modify the echo
statement and theme to your liking.
$path = current_path();
$nids = skl_get_all_menu_node_children_ids($path);
$children = node_load_multiple($nids);
foreach($children as $c) {
echo $c->title . ': ' . url('node/' $c->nid) . '<br />';
}
And here's the function's full code. Check the link for possible future updates.
Good luck.
/**
* Returns node ids of all the child items, including children of children
* on all depth levels, of the given node path. Returns an empty array
* if any error occurs.
*
* @param string $node_path
* @return array
*/
function skl_get_all_menu_node_children_ids($node_path) {
//Stop and return an empty array if node path is empty
if(empty($node_path)) {
return array();
}
//Init empty array to hold the results
$nids = array();
//Init parent keys. Check 'foreach' loop on parent keys for more info.
$parent_keys = array('plid', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9');
//Collect menu item corresponding to this path to begin updates.
//Reference: http://stackoverflow.com/a/11615338/136696
//Note: we couldn't find a way to get the sub-tree starting from this item
//only and hence we had to get the whole menu tree built and then loop on
//the current item part only. Not so bad considering that Drupal will
//most probably have the whole menu cached anyway.
$parent_menu_item = menu_link_get_preferred($node_path);
//Stop and return empty array if a proper current menu item couldn't be found
if(empty($parent_menu_item['menu_name']) || empty($parent_menu_item['mlid'])) {
return array();
}
//Init parent item mlid for easier usage since now we know it's not empty
$parent_menu_item_mlid = $parent_menu_item['mlid'];
//Build whole menu based on the preferred menu_name gotten from this item
$menu = menu_build_tree($parent_menu_item['menu_name']);
//Reset menu cache since 'menu_build_tree' will cause trouble later on after
//you call pathauto to update paths as it can only be called once.
//Check: https://www.drupal.org/node/1697570
menu_reset_static_cache();
//Init processing array. This will hold menu items as we process them.
$menu_items_to_process = array();
//First run to fill up the processing array with the top level items
foreach($menu as $top_level_menu_item) {
$menu_items_to_process[] = $top_level_menu_item;
}
//While the processing array is not empty, keep looping into lower
//menu items levels until all are processed.
while(count($menu_items_to_process) > 0) {
//Pop the top item from the processing array
$mi = array_pop($menu_items_to_process);
//Get its node id and add it to $nids if it's a current item child
//Note that $parent_keys contains all keys that drupal uses to
//set a menu item inside a tree up to 9 levels.
foreach($parent_keys as $parent_key) {
//First, ensure the current parent key is set and also mlid is set
if(!empty($mi['link']['mlid']) && !empty($mi['link'][$parent_key])) {
//If the link we're at is the parent one, don't add it to $nids
//We need this check cause Drupal sets p1 to p9 in a way you
//can easily use to generate breadcrumbs which means we will
//also match the current parent, but here we only want children
if($mi['link']['mlid'] != $parent_menu_item_mlid) {
//Try to match the link to the parent menu item
if($mi['link'][$parent_key] == $parent_menu_item_mlid) {
//It's a child, add it to $nids and stop foreach loop.
//Link_path has the path to the node. Example: node/63.
if(!empty($mi['link']['link_path'])) {
$nids[] = str_replace('node/', '',
$mi['link']['link_path']);
break;
}
}
}
}
}
//Add its child items, if any, to the processing array
if(!empty($mi['below']) && is_array($mi['below'])) {
foreach($mi['below'] as $child_menu_item) {
//Add child item at the beginning of the array so that when
//we get the list of node ids it's sorted by level with
//the top level elements first; which is easy to attain
//and also useful for some applications; why not do it.
array_unshift($menu_items_to_process, $child_menu_item);
}
}
}
//Return
return $nids;
}
$tree = menu_build_tree('main-menu', array( 'expanded' => array($trail[1]['mlid']) ));
drupal_render(menu_tree_output($tree))
Then using CSS I can style the links to remove theul
padding, making them appear they are all on the same level. Not ideal, but effective. EDIT: sorry, I can't figure out how to get line breaks to work.get_sub_menu_based_on_active_page()
and I'm all set. I had to move on from trying to figure out the nesting issue as css makes the user none the wiser.