2010-12-16 115 views

回答

1

菜單系統被緩存,所以你不能添加或刪除菜單項,因爲你根據用戶,頁面瀏覽,自定義邏輯等,這是你不能做到這一點,而不必清除菜單緩存會導致嚴重的性能下降。

您可以做什麼,創建這種效果是創建一些自定義邏輯來定義菜單項上的訪問控制。由於Drupal隱藏了用戶無權訪問的菜單項,因此您可能在某些情況下拒絕了隱藏菜單項的權限。這是一個有點駭人聽聞的解決方案。

我更喜歡的另一種解決方案是使用js或css來隱藏或顯示菜單。您可以動態地在主體上添加/刪除一個類,以確定菜單項是否應該顯示。但是,如果您需要這些類型的菜單項中的幾個,這將很快變得難以管理。

+1

爲什麼你認爲它的hackish隱藏使用訪問系統菜單項? Drupal核心也可以通過登錄/註銷鏈接來做到這一點,我沒有看到任何問題(除非你隱藏了用戶通過另一個鏈接訪問的內容)。 – marcvangend 2010-12-16 14:07:14

+0

@marcvangend:它取決於你如何使用它,如果你寫了一個自定義的訪問函數來拒絕訪問一個頁面,當你在某些其他頁面時,我會打電話給那個黑客。它是有道理的,你不能登錄時,你沒有登錄,但不是你不能訪問一個頁面,當你在不同的頁面上。該檢查本身沒有意義,並且純粹用於隱藏菜單項。 – googletorp 2010-12-17 14:19:11

+0

不能一個人也使用(是的,雖然再次,黑客 - 但 - 使用了很多)CSS簡單地'隱藏'基於特定的CSS已經存在的HTML標記?一點.js也可以。有時(甚至我)忘記我們有這麼多的媒體來操縱DOM :)。 – 2012-09-11 16:20:03

3

您需要在模塊中實現hook_menu。例如:

<?php 
function mymodule_menu() { 
    $items['mymodule/links'] = array(
    'title' => 'Links', 
    'page callback' => 'mymodule_links_page', 
    'access arguments' => array('access content'), 
    'type' => MENU_SUGGESTED_ITEM, 
); 
    return $items; 
} 
?> 

'type' => MENU_SUGGESTED_ITEM,部分使得可選的,因此它可以由最終用戶啓用 - 是你與「有條件」是什麼意思?如果沒有,請解釋您要尋找哪種「有條件的」。

2

或者您可以使用'type' => MENU_NORMAL_ITEM,,因爲它默認啓用,但可以隨時禁用。這當然取決於你的喜好。請參閱http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7以作進一步參考。

在定製菜單中使用模塊定義的菜單項時,另一件好事是知道如何以編程方式創建想要使用的菜單,以便創建「開箱即用」的菜單。只需添加一個mymodule.install文件,你把下面的代碼:

<?php 
function mymodule_install() { 
    $menu = array( 
    'menu_name' => 'links', 
    'title' => 'My Custom Links', 
    'description' => 'Descriptive text.', 
); 
    menu_save($menu); 
} 
?> 

如果你有一個卸載功能,不要忘了,不僅停用模塊,同時也將其卸載。重新啓用模塊,刷新緩存,菜單項應該在那裏!

2

您可以根據條件(訪問回調)動態顯示或隱藏菜單項。

這裏是https://drupal.org/project/examples一個例子:

<?php 
function mymodule_menu() { 
    $items = array(); 

    $items['my-menu-item'] = array(
    'title' => 'My Menu', 
    'description' => 'My description', 
    'page callback' => 'my_page_link_callback_function_name', 
    'access callback' => 'can_the_user_see_this_item', 
    'expanded' => TRUE, 
    'weight' => -100, 
    'menu_name' => 'primary-links', 
); 

    return $items; 
} 

// Here we determine if the user can or can not see the item. 
function can_the_user_see_this_item(){ 
    if (MY_CONDITION){ 
    return TRUE; 
    } 
    else { 
    return FALSE; 
    } 
} 
1

使用menu_link_save()功能

Saves a menu link. 

After calling this function, rebuild the menu cache using menu_cache_clear_all(). 

Parameters 

$item: An associative array representing a menu link item, with elements: 

link_path: (required) The path of the menu item, which should be normalized first by calling drupal_get_normal_path() on it. 
link_title: (required) Title to appear in menu for the link. 
menu_name: (optional) The machine name of the menu for the link. Defaults to 'navigation'. 
weight: (optional) Integer to determine position in menu. Default is 0. 
expanded: (optional) Boolean that determines if the item is expanded. 
options: (optional) An array of options, see l() for more. 
mlid: (optional) Menu link identifier, the primary integer key for each menu link. Can be set to an existing value, or to 0 or NULL to insert a new link. 
plid: (optional) The mlid of the parent. 
router_path: (optional) The path of the relevant router item. 
$existing_item: Optional, the current record from the {menu_links} table as an array. 

$parent_candidates: Optional array of menu links keyed by mlid. Used by _menu_navigation_links_rebuild() only. 

Return value 

The mlid of the saved menu link, or FALSE if the menu link could not be saved. 
相關問題