노드의 경로가 지정된 하위 메뉴 항목을 가져 오는 기능을 게시했습니다. 여기에서 확인할 수 있습니다 : http://softkube.com/blog/getting-child-menu-items-drupal-menu-tree
게시물이 업데이트 될 경우 향후 답변에 대한 링크를 포함하고 있으며 마지막에 전체 코드를 복사 / 붙여 넣습니다.
귀하의 경우 테마에서 이와 같은 것을 실행하여 모든 하위 메뉴 항목을 나열 할 수 있습니다. echo
취향에 맞게 설명과 주제를 수정하십시오 .
$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 />';
}
그리고 여기 함수의 전체 코드가 있습니다. 향후 업데이트 가능성에 대한 링크를 확인하십시오.
행운을 빕니다.
/**
* 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))
그런 다음 CSS를 사용하여 링크의 스타일을 지정하여ul
패딩 을 제거하여 모두 동일한 레벨에 표시되도록 할 수 있습니다. 이상적이지는 않지만 효과적입니다. 편집 : 죄송합니다, 줄 바꿈이 작동하는 방법을 알 수 없습니다.