views:

678

answers:

2

How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!

I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.

I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:

<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php        
        $href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
        print "<a href='" . $href . "'>" . $link['title'] . "</a>";            
        ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.

I sincerely thank you in advance to lessening my gargantuan headache!

+4  A: 

It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.

This is one I used to alter the classes of my primary links:

function primary_links_add_icons() {
  $links = menu_primary_links();
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  $output = "<ul class=\"links-$level\">\n";   
  if ($links) {
    foreach ($links as $link) {
        $link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
        $output .= '<li class="sublevel">' . $link .'</li>';
    };
    $output .= '</ul>';
  }
  return $output;
}

And then in page.tpl.php I just called it like this:

<?php if ($primary_links) :?>
    <?php print '<div id="menu">'; ?>
    <?php print primary_links_add_icons(); ?>
    <?php print '</div>'; ?>
<?php endif;?> 
hfidgen
Now does this effect all menus within Drupal? One of the issues that I was experiencing when overwriting the links like this was that it even effected the links within my sidebar... annoying.
dcolumbus
No this should just affect your Primary Links - as the template.php function just deals with those. Did you use this code and was that the effect?
hfidgen
I apologize... I've see some many similar functions over the past week that it all blends together in my mind. "primary_links_add_icons" only effects the primary links, yes. Thank you.
dcolumbus
No probs - I've received a lot of advice on this site, so it's nice to be able to give something back!
hfidgen
+1  A: 

I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed. This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.

You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently. MarkLNH

Mark
Is this a good practice for Drupal 7 Themes as well?
dcolumbus

related questions