Hey guys.

I know this information exists somewhere (it can't not!), but I can't find it for the life of me. I've been searching here and some other Drupal theme sites, and I can't find what I'm after.

I'm making a theme, which doesn't need to be too flexible. I'm using Abessive as a base, but really I'm not sharing any of the CSS or HTML, I just used it to learn the anatomy of the theme. I was wondering where the HTML is for the primary and secondary links. I really need to get in and modify the way links display, using images and all that jazz. Where is that, or what's the best way to write it?

As I'm sure the information exists, if anyone has a link to it, that'd be absolutely fantastic.

Thanks a lot!

Comments

elmasry-1’s picture

I had a similar assigment, where I had to change the behaviour of the primary-links menu. I needed to show a multi level menu in the primary links
Basically in page.tpl.php change:

<?php if (isset($primary_links)) { print theme('links', $primary_links, array('class' =>'primary-links', 'id' => 'navlist')) } ?>

to

<?php if (isset($primary_links)) { print get_rendered_primary_links() } ?>

where get_rendered_primary_links is my function, and then I added the following in template.php:

<?php
//function get_rendered_primary_links($links, $attributes = array('class' => 'links')) {
function get_rendered_primary_links() {
  
  //Compiling our own list, since I needed the pid, which was not available through the $links
  $links = get_menu_primary_links();
  
  if (!count($links)) {
    return '';
   }
  return menu_create_menu($links, TRUE );
}
function menu_create_menu($links, $firstCall=FALSE) {
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  if ( $firstCall ) {
    $output = "<ul class=\"links-$level primary-list\" id=\"navlist\">\n";
  } else {
    $output = "<ul class=\"links-$level primary-list\">\n";
  }
  
  
  foreach ($links as $index => $link) {
    $output .= '<li';
    if (stristr($index, 'active')) {
      $output .= ' class="active"';
    }
    $output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
    if ( count($link['children']) > 0 ) {
    	$output .= menu_create_menu($link['children']);
    }
    $output .="</li>\n";
  }
  $output .= '</ul>';
  return $output; 
}
/**
 * build the primarz links array
 */
function get_menu_primary_links($start_level = 1, $pid = 0) {
	
  if (!module_exists('menu')) {
    return NULL;
  }
  if (!$pid) {
    $pid = variable_get('menu_primary_menu', 0);
  }
  if (!$pid) {
    return NULL;
  }

  if ($start_level < 1) {
    $start_level = 1;
  }

  if ($start_level > 1) {
    $trail = _menu_get_active_trail_in_submenu($pid);
    if (!$trail) {
      return NULL;
    }
    else {
      $pid = $trail[$start_level - 1];
    }
  }

  $menu = menu_get_menu();
  $links = array();
  if ($pid && is_array($menu['visible'][$pid]) ) {
    $count = 1;
    foreach ($menu['visible'][$pid]['children'] as $cid) {
    	//get_menu_primary_links($start_level + 1, $cid);
      $index = "menu-$start_level-$count-$pid";
      if (menu_in_active_trail_in_submenu($cid, $pid)) {
        $index .= "-active";
      }
      $links[$index] = get_menu_item_links($cid);
      $count++;
    }
  }
  return $links;
}

/**
 * get item link
 */
function get_menu_item_links($mid) {
  $item = menu_get_item($mid);
  $link_item = $item;
  $link = '';

  /*while ($link_item['type'] ) {
  	print 'has children';
    $link_item = menu_get_item($link_item['pid']);
  }*/
 
  $children = array();
  $counter = 0;
  if (count($item['children']) > 0 ) {
    foreach ($item['children'] as $index => $value ) {
	  $children[$counter] = get_menu_item_links($value);
	  $counter++;
	}
  }
  
  $link = array(
    'title' => $item['title'],
    'href' => $link_item['path'],
    'pid' => $mid,
    'children' => $children,
    'attributes' => !empty($item['description']) ? array('title' => $item['description']) : array()
  );

  return $link;
}
?>

Edited by: VeryMisunderstood; Corrected closing code tags

methy’s picture

Thanks! That's exactly what I was after!

Mike_Waters’s picture

Shouldn't this be done in either links.tpl.php or phptemplate_links()/mytheme_links()?
And, to add data that is not already available through $links, shouldn't you use a preprocess function? (phptemplate_preprocess_links())

I am really asking; with all the ways that it is possible to do this in Drupal, shouldn't the above be the 'Drupal way'?

I also have a problem with using theme('links' ...) in a template file; shouldn't the 'separation of interface and logic' model dictate that the $links (primary_links, etc.) be directly available to the template files? Does anyone know why this is not the case? It is easy to change this in your theme, using a preprocess function, but it just doesn't seem right.

Thanks for your input.

methy’s picture

I'll look into your suggestions. I'm actually having trouble finding the code that outputs the links, so I'm not exactly certain of what to do here.

arisk2’s picture

Elmasry,
Thanks for sharing the code, but I cant get it to work.
I replaced the function call on page.tpl.php and added the code to template.php but the menu doesn't show up at all.
This is on a modified A3 atlantis theme.
Any help is appreciated.