4

I need to create a link to first submenu item from specific menu. The link will be placed in custom block.

To be more precise, I have a menu with following structure:

parent-menu
 item-1
 item-2
 item-3
 ..

I need that after clicking "parent-menu" user would end up in item-1 page. Also, there will be several links in some blocks, linking to "parent-menu" - they need to open 'item-1' item too.

My current approach is to create 'parent-menu' page template override which finds top child item from 'parent-menu'. That page will be linked from content and main menu. Problem is that I need a way to find first menu sub item from code.

UPDATE:

Ended up on using query:

$link_path = db_select('menu_links', 'n')
->fields('n', array('link_path'))
->condition('n.plid', '269', '=')
->orderBy('weight', 'ASC')
->range(0, 1)
->execute()
->fetchField();

Is there any alternative way to do this, without querying database?

1

I found this module Menu Firstchild maybe it will do what you want it to do.

1
  • Sadly, this module works with menu only, and it is impossible (or I just don't know how) to create a link in content pointing to same <firstchild> item. – joshas Apr 4 '12 at 14:51
0

You can use the ctools page manager to do a redirect. ( Page manager is basically a GUI for hook_menu_alter(); ) If you aren't familiar with it, the quickest way to learn it is to watch Johan Falk's screencast series at Nodeone.se

Using page manager, you can easily redirect one page to another.

Sub-menu Tree is another module you may want to try out. It will make a page for the parent menu item that lists all of the child items.

0

I have added this piece of code at the bottom of my tepmlate file (before closing body), it takes the name and target of the parent link and then injects it as the first item in the sub-menu. It will work with morst browsers, what was good enough for my client.

<script type="text/javascript"> 
(function ($) {     
  $(document).ready(function() { // Re-create the first child in a menu to mimic the main link
    var me = $(".menu li .menu li.first");
    var parent = me.parent().parent();
    me.before('<li class="'+parent.attr('class')+'"><a href="'+parent.find('a').attr('href')+'" class="'+parent.find('a').attr('class')+'">'+parent.find('a').html()+'</a></li>');
  });
}) (jQuery); 
 </script>  
0

You can use menu_tree() and menu_link() menu_tree creates the <ul> wrapping while menu_link creates the menu list items <li>

Inside menu_link() you can find $submenu, this contains an array with the submenu. This function return this <li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";

Cheking the variable $element you can find the all the things for the list item i.e. Title, link etc...

So, if you want to do this for all the link in the first level:

if ($variables['element']['#original_link']['depth'] == 1)

or you can find the objects that say "has a sub menu and it's expanded"

Then you can rewrite your $output giving the item name with the first submenu item link.

I hope it's clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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