2014-10-28 7 views
0

menu_menu_link를 사용하여 메뉴를 수정합니다 (이 경우에는 main_menu). Evertything은 예상대로 작동하지만 메뉴 블록을 사용하여 내부 페이지의 사이드 바에있는 기본 메뉴의 다른 인스턴스를 렌더링합니다.Drupal - them_menu_link를 사용하여 메뉴 인스턴스를 하나만 수정하는 방법

문제는 사이드 바에서 사용하고있는 인스턴스가 아니라 사이트의 기본 탐색에 사용하는 main_menu의 인스턴스에만 내 수정 사항이 표시되기를 원합니다.

세부 사항 :

여기 내 theme_menu_link 기능

/** 
* Returns custom HTML for the main navigation menu. 
* Adds an HTML element to top level parent <li> elements 
* to serve as a drop-down menu toggle. 
* 
* this also sets the HTML wrapper for sub menus, to override the above 
* them_menu_tree function, which is intended for top level menus only. 
* 
* @param $variables 
* An associative array containing: 
* - element: Structured array data for a menu link. 
* 
* @ingroup themeable 
*/ 
function mytheme_menu_link__main_menu($variables) { 
    $element = $variables['element']; 
    $child_menu = $element['#below']; 
    $sub_menu = ''; 

    // if the current top level item has child menus (sub menus) 
    if ($child_menu) { 
    // unset the sub menu wrapper set above in mytheme_menu_tree__new_main_menu() 
    unset($child_menu['#theme_wrappers']); 

    // add opening ul tag for the sub menu wrapper to $sub_menu 
    $sub_menu = '<ul class="menu sub-menu">'; 

    // iterate over each sub menu item 
    foreach ($child_menu as $child_menu_item){ 

     // add sub menu item link HTML to a variable 
     $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']); 

     // check to see if item has a title, sincle $element['#below'] returns things besides menu items 
     if($child_menu_item['#title']) { 
      // output each sub menu item's link and description, wrapped in a <li> element 
      $sub_menu .= '<li>' . $child_menu_output .'</li>'; 
     } // end if shild menu item has a title 

    } // end foreach child menu item 

    // add closing ul tag for the sub menu wrapper to $sub_menu 
    $sub_menu .= '</ul>'; 
    } 

    // output a dummy link for top level items 
    $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>'; 
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; 

} 

위의 함수가 나는 그것이 사이트의 기본 탐색 작업 할 것처럼 작동합니다. 그러나, 메뉴 모음과 함께 사이드 바에서 사용하고있는 main_menu의 인스턴스에 적용하지 않기를 바랍니다. 거기에 조건에 따라 them_menu_link를 조건부로 사용하는 방법이 있나요? 아니면 메뉴가 빙에 렌더링 되었습니까?

+0

이 메뉴는 당신을 위해 일을 속성하지 않는 이유는 무엇입니까? https://www.drupal.org/project/menu_attributes – hyunkeln

+0

나는 그 모듈을 보았지만, 내가하려고하는 것을 다루지 않는 것 같다. 메뉴 항목 속성뿐만 아니라 메뉴 마크 업을 커스터마이징한다. 특정 메뉴의 단일 인스턴스에 대한 사용자 정의 내 대답은 아래 작품. –

답변

0

그래서이 문제를 해결하기위한 방법을 찾았습니다. 그러나 최선의 방법인지는 확실하지 않습니다.

I 첫번째 항목은 메뉴에있는 블록 $variables['element']['#theme'], 각 메뉴 배열을 갖는 것을 알 수 있었다 theme_menu_link 함수 내에 krumo ($ 변수)를 사용하여 메인 네비게이션 : 그래서., $variables['element']['#theme'][0] === 'menu_link__menu_block__1' 및 사이드 바 탐색의 경우 : $variables['element']['#theme'][0] === 'menu_link__menu_block__2'.

mytheme_menu_link__main_menu() 함수에서 if/else를 만드는 것이 어렵지 않은 것을 보았을 때 (최상위 링크에 대해서는 a href="#" 출력) 메인 메뉴에 대한 사용자 정의는 수행하지만 사이드 바 메뉴에는 그렇지 않습니다. .

그래서 새롭고 개선 된 theme_menu_link 기능은 다음과 같습니다

function mytheme_menu_link__main_menu($variables) { 
    $element = $variables['element']; 
    $sub_menu = ''; 
    // Only customize the menu instance if its the main navigation, not sidebar navigation, etc. 
    // We need to check which block the menu is in, otherwise all instances of main_menu will get this treatment. 
    if($element['#theme'][0] === 'menu_link__menu_block__1'){ 
    $child_menu = $element['#below']; 
    // if the current top level item has child menus (sub menus) 
    if ($child_menu) { 
     // unset the sub menu wrapper set above in mytheme_menu_tree__new_main_menu() 
     unset($child_menu['#theme_wrappers']); 

     // add opening ul tag for the sub menu wrapper to $sub_menu 
     $sub_menu = '<ul class="menu sub-menu">'; 

     // iterate over each sub menu item 
     foreach ($child_menu as $child_menu_item){ 

     // add sub menu item link HTML to a variable 
     $child_menu_output = l($child_menu_item['#title'], $child_menu_item['#href']); 

     // check to see if item has a title, sincle $element['#below'] returns things besides menu items 
     if($child_menu_item['#title']) { 
      // output each sub menu item's link and description, wrapped in a <li> element 
      $sub_menu .= '<li>' . $child_menu_output .'</li>'; 
     } // end if shild menu item has a title 

     } // end foreach child menu item 

     // add closing ul tag for the sub menu wrapper to $sub_menu 
     $sub_menu .= '</ul>'; 
    } 

    // output a dummy link for top level items 
    $output = '<a href="#" class="nolink">' . $element['#title'] . '</a>'; 
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; 
    } 

    // Otherwise if we're not in the main navigation block, output a normal menu 
    else { 
     if ($element['#below']) { 
     // unset the sub-menu wrapper set above in mytheme_menu_tree__main_menu() 
     unset($element['#below']['#theme_wrappers']); 

     // Add a custom wrapper for sub-menus for CSS sanity and profit 
     $sub_menu = '<ul class="menu sub-menu">' . drupal_render($element['#below']) . '</ul>'; 
     } 

     $output = l($element['#title'], $element['#href'], $element['#localized_options']); 
     return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>'; 
    } 
} 
+1

다음을 시도해 볼 수도 있습니다 :'global $ _current_region; if (isset ($ _ current_region) && $ _current_region == 'REGION_NAME') { // YOUR CODE } ' – Djouuuuh

+0

고마워! 이 솔루션은 특정 블록 이름에만 국한되지 않고 훨씬 논리적이고 유연한 지역에 훨씬 더 좋습니다. –