开发者

How to alter a MENU_LOCAL_TASK tab menu in Drupal

I have built a tabbed menu in my custom Drupal 6 module. I want to position an html dropdown list to the right of the tabbed menu at the top of my module page. The list will fire some ajax events on change e.g. changing the LIMIT clause on SQL qu开发者_如何学Goery by specifying 10,20,50,100 results. How can I achieve this in Drupal without hacking templates?

Thanks,


You could do this by overriding theme_menu_local_tasks() within your theme:

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;
}

(NOTE: Untested code - potential typos)


As you are referring to code to put in a module, then the module should implement hook_theme_registry_alter(), which would allow the module to override the function theme_menu_local_tasks(). The module should store the value of the previous callback, so that it could still call it in the case the page it not one that it should change.
Implementing a hook in the module allows you to have the normal menu tabs, once the module has been disabled; altering the current theme would require you to change it back when you want the functionality anymore, and if you are using a theme made from another person you should change the theme all time you download a new version. If you are using more than one theme, then you should make the change to each used theme.
In general, a modification to a theme that is required from a module should be done inside a module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜