2009-12-01 36 views
4

我在自定義Drupal 6模塊中構建了選項卡式菜單。我想在模塊頁面頂部的選項卡式菜單右側放置一個html下拉列表。該列表將在變化時觸發一些ajax事件,例如通過指定10,20,50,100個結果來更改SQL查詢中的LIMIT子句。如何在Drupal中實現這個功能而不需要黑客模板?如何更改Drupal中的MENU_LOCAL_TASK選項卡菜單

感謝,

回答

6

你可以通過你的主題範圍內覆蓋theme_menu_local_tasks()做到這一點:

function yourTheme_menu_local_tasks() { 
    // Prepare empty dropdown to allow for unconditional addition to output below 
    $dropdown = ''; 
    // Check if the dropdown should be added to this menu 
    $inject_dropdown = TRUE; // TODO: Add checking logic according to your needs, e.g. by inspecting the path via arg() 
    // Injection wanted? 
    if ($inject_dropdown) { 
    // Yes, build the dropdown using Forms API 
    $select = array(
     '#type' => 'select', 
     '#title' => t('Number of results:'), 
     '#options' => array('10', '20', '50', '100'), 
    ); 
    // Wrap rendered select in <li> tag to fit within the rest of the tabs list 
    $dropdown = '<li>' . drupal_render($select) . '</li>'; 
    } 

    // NOTE: The following is just a copy of the default theme_menu_local_tasks(), 
    // with the addition of the (possibly empty) $dropdown variable output 
    $output = ''; 
    if ($primary = menu_primary_local_tasks()) { 
    $output .= "<ul class=\"tabs primary\">\n". $primary . $dropdown . "</ul>\n"; 
    } 
    if ($secondary = menu_secondary_local_tasks()) { 
    $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n"; 
    } 

    return $output; 
} 

(注:未經測試的代碼 - 潛在的錯別字)

+0

不錯(+1)。唯一不清楚的是爲什麼你必須定義'$ dropdown ='';'。如果'$ dropdown'未初始化,AFAIK連接操作已經將'$ dropdown'轉換爲''。 (...或者我錯過了什麼)? – mac 2009-12-02 09:02:08

+0

@mac:你說的對,沒有必要。我想這只是我的一個深層次的習慣,使用更嚴格的類型化語言來避免使用未分配的/ NULL變量,而不是直接「isset」檢查;) – 2009-12-02 09:27:10

0

當你指的是代碼投放一個模塊,那麼模塊應該實現hook_theme_registry_alter(),這將允許模塊覆蓋功能theme_menu_local_tasks()。模塊應該存儲前一個回調的值,以便它可以在頁面不是應該改變的情況下調用它。
在模塊中實現鉤子後,您可以擁有正常的菜單選項卡,一旦模塊被禁用;如果要更改當前主題,則需要在需要該功能時將其更改回來;如果使用的是另一個人製作的主題,則在下載新版本時應更改主題。如果您使用多個主題,則應對每個使用的主題進行更改。
通常,應該在模塊內部對模塊所需的主題進行修改。