I think the rigid structure of the menu system is the only glaring inconsistency in an otherwise great piece of sotftware. I'm currently developing a University website based on drupal but need to customize the menu some and finding it very difficult to create a standards-compliant css drop-down menu.

At drupaldocs there is plenty of reference but no examples. and the existing examples on drupal.org; Overriding other theme functions, contaire.com article, and Protecting Content don't tell a designer exactly what to do.

I would like to place a horizontal custom menu in a specific place in page.tpl.php.

The contAire article shows how to accomplish this for the phpTal engine, but I don't get how it's supposed to work for the phpTemplate engine.

I understand that the additional function based on theme_menu_tree needs to go in template.php, but does it go within class Template and how do you write the code to access a specific menu in my page template (using phpTemplate engine)? (I thought it would be based in the Menu ID but in the article they use the pid )..

Thanks in advance for any help :-)

thanks,
Brett

Comments

dors’s picture

I would also like to know how I could override the theme_menu_tree function using phpTemplate, but right now I don't have a clue as to how to do this.

I understand that is has something to do with declaring a phptemplate_menu_tree function in template.php and using a menu_tree.tpl.php template then, but as to how declare/implement that I have no idea. Any help would be appreciated!

BTW, I'm only trying to get rid of the

bryan kennedy’s picture

So if you wanted to style the theme_menu_tree call in phptemplate, then add this function to your "template.php" file in your theme directory.

template.php

function phptemplate_menu_tree($pid = 1, $all = FALSE) {
	return _phptemplate_callback('menu_tree', array('pid' => $pid, 'all' => $all));
}

Then create a file called ''menu_tree.tpl.php" and place it in the same directory

menu_tree.tpl.php

  $menu = menu_get_menu();
  $output = '';

  if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {

    foreach ($menu['visible'][$pid]['children'] as $mid) {
      $style = (count($menu['visible'][$mid]['children']) ? (menu_in_active_trail($mid)  ? 'expanded' : 'collapsed') : 'leaf');
      $output .= "<li class=\"$style\">";
      $output .= theme('menu_item', $mid);
      if ($all || menu_in_active_trail($mid)) {
        $output .= theme('menu_tree', $mid);
      }
      $output .= "</li>\n";
    }

    if ($output != '') {
      $output  = "\n<ul>\n$output\n</ul>\n";
    }
  }
  echo $output;

Then just modify the ''menu_tree.tpl.php" file however, you want. I have found it usefull to have a second browser open that you are not logged in as to see the changes. That seems to prevent you from having to visit the themes admin page every time to get the changes to take effect. If this doesn't help, post back.

bryan kennedy


Help us to better document Drupal. Join the Dupal Documentation Mailing List

dors’s picture

Thanks a lot, it works and is at least getting me started!

dors’s picture

Hm, I just realised that <div class="menu"> (which is what I wanted to get rid off) is hard-coded in menu.module. Shouldn't that be moved into the theme functions?

Steven’s picture

A div with a class is a neutral element. You can control it completely through CSS.

--
If you have a problem, please search before posting a question.

gordon’s picture

So that you don't need to modify the phptemplate engine, I have have written a generic subroutine which allows you to just create the menu_tree.tpl.php file and then it will just start getting called. This means that in the future you can run with a completely standard phptemplate engine.

see http://drupal.org/node/16409

I am still trying to get it into standard, so any help would be appreciated.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

bryan kennedy’s picture

gordon, this would be so cool if this could get included in 4.6. The level of PHP discussion you are having on the feature request is a little above my head so I can't say I can help in any real way. However, I deffinately can add a vote of confidence. This would be so nice to not have to modify the template file each time I just wanted to theme a new function. +1!


Help us to better document Drupal. Join the Dupal Documentation Mailing List
gordon’s picture

I am glad that you like it, if you can put the +1 against that so it can be put into 4.6 would be great.

I actually did it so that I can use the menu.module menus to do the primary and secondary menus for my theme instead of the phptemplate way which sucks, and then use phplayersmenu to create pulldown menus.

I am most of the way there so I can have it completely done in the theme instead of using my navtree module.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

Gunny-1’s picture

If i have a function called dev_menu which contains the entire code in menu_tree.tpl.php.
in template.php i changed the functions as

function phptemplate_menu_tree($pid = 1) {
    return _phptemplate_callback('menu_tree', array('pid' => $pid, 'try' => dev_menu($pid)));
}

then in page.tpl.php as

print phptemplate_menu_tree(whatever_parent_menu_id_from_menu_table)

got an error,
Call to undefined function: phptemplate_menu_tree() in themes/box_grey/page.tpl.php on line 31

suggestions

Gunny-1’s picture

here is dev_menu


function dev_menu($pid = 1) {
  $menu = menu_get_menu();
  $try = array();
  if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children'])
{
    foreach ($menu['visible'][$pid]['children'] as $mid) {
      $style = (count($menu['visible'][$mid]['children'])
        ? (menu_in_active_trail($mid)
          ? 'expanded' : 'collapsed')
          : 'leaf');
      $entry = array('style' => $style, 'link' => theme('menu_item', $mid));
      $entry['kids'] = dev_menu($mid);
      $try[] = $entry;
    }
  }
  return $entries;
}