2009-12-01 38 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()。模块应该存储前一个回调的值,以便它可以在页面不是应该改变的情况下调用它。
在模块中实现钩子后,您可以拥有正常的菜单选项卡,一旦模块被禁用;如果要更改当前主题,则需要在需要该功能时将其更改回来;如果使用的是另一个人制作的主题,则在下载新版本时应更改主题。如果您使用多个主题,则应对每个使用的主题进行更改。
通常,应该在模块内部对模块所需的主题进行修改。