3

My primary navigation is hard coded (it has to be; graphical purposes).

Firstly, how can I tell which one is "active" (current page)?

Secondly, based upon which one is active, how can I show child links of that link in the sidebar? I've thought of a possibility, but it would require creating tons of blocks for the menus and then only enabling them on their parent page. This seems like a lot of work for something that a module should take care of.

I've found http://drupal.org/project/menu_block which looks promising, but I'm not clear how to use it in my situation.

I prefer to have total control of how the menus will look (a custom .tpl file)

3
  • what graphical purposes require hard coded navigation? I'll tell you from personal experience that gets to be a HUGE frustration
    – Chaulky
    Commented Jun 9, 2011 at 4:27
  • @Chaulky - The navigation is fixed width and pushing the limits on the pixel width (if it goes over one pixel then the whole navigation will break). It's only images (no text).
    – Matthew
    Commented Jun 9, 2011 at 4:57
  • I would suggest you ask one question per question, not two.
    – berkes
    Commented Jun 9, 2011 at 8:59

2 Answers 2

0

This code will show the current child links:

    $mn = menu_tree_page_data($menu-name);
    foreach ($mn as $menu) {
      if (!empty($menu['link']['in_active_trail']) AND is_array($menu['below']) AND !is_null($menu['below'])) {
        echo menu_tree_output($menu['below']);
      }
    }
1

I am not sure which part you need hardcoded, but if it is everything but the a tag, then then l() helper function comes to the rescue:

<li class="menu-item" id="foo">
  <span>
    <img src="menu_foo.png" alt="icon showing foo"/>
    <?php print l("Foo", "original/path/to/foo") ?>
  </span>
</li>

Now, if you access "/original/path/to/foo" or (on of its) alias(es), the l() helper will automatically add a class "active" to the a tag, generated by l().

If, for some reason you need the l() outside of the a, or if you want to customize the a itself (e.g. by adding markup to it), you can use the code seen in the l() to determine activeness of the current item. Place this as helper in template.php of your theme:

function _my_theme_is_active($path) {
  $active = FALSE;
  if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) && 
      (empty($options['language']) || $options['language']->language == $language->language)) {
    $active = TRUE;
  }
  return $active;
}

Obviously my_theme is the name of your theme.

Then in the tpl.php files, or anywhere in the scope of your theme, you can use the function:

<li>
  <a href="<?php print url("original/path/to/foo") ?>" <?php print (_my_theme_is_active("original/path/to/foo") ? 'class="active"' : ' ') ?> id="foo-1">
    Foo
  </a>
</li>

Beware that you use url() to catch aliasing for the oringial path, but that you /do/ pass in the original (system) path into the _my_theme_is_active helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.