views:

1272

answers:

2

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 query by specifying 10,20,50,100 results. How can I achieve this in Drupal without hacking templates?

Thanks,

+3  A: 

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)

Henrik Opel
Nice (+1). The only thing that is not clear to me is why you have to define `$dropdown = '';`. AFAIK the concatenation operation already turn `$dropdown` into '' if `$dropdown` is not initialized. (...or am I missing something)?
mac
@mac: You are right, it is not necessary. I guess it is just a deep seated habit of mine from working with more strictly typed languages to avoid usage of unassigned/NULL variables for anything else than a direct 'isset' check ;)
Henrik Opel
A: 

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.

kiamlaluno

related questions