Menu splitting into blocks
You might like to have your primary links and their children split into two different blocks, which you might like to have on two different columns. This is a way to do so for drupal 5.x:
Create a new block with this code in it:
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'));
}
return $output;
?>This will give you a first block with the first level of your primary links.
Then create a second block with this code:
<?php
$links = menu_primary_links(2);
if ($links) {
$key = key($links);
$ekey = explode('-', $key);
$pid = $ekey[3];
$output = theme_menu_tree($pid);
return $output;
}
?>This block will show the second level and any child level for the active primary links menu.
An alternate solution would be to override the theme_links function.
(drupal 5.1) In template.php:
<?php
function phptemplate_links($links, $attributes = array('class' => 'links'), $start = 1, $end = 0) {
$output = '';
if (count($links) > 0) {
if ($end==0) $end = count ($links);
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
if (($i>=$start) && ($i<=$end)) {
$class = '';
// Automatically add a class to each link and also to each LI
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' ' . $key;
$class = $key;
}
else {
$link['attributes']['class'] = $key;
$class = $key;
}
// Add first and last classes to the list of links to help out themers.
$extra_class = '';
if ($i == 1) {
$extra_class .= 'first ';
}
if ($i == $num_links) {
$extra_class .= 'last ';
}
$output .= '<li class="'. $extra_class . $class .'">';
// Is the title HTML?
$html = isset($link['html']) && $link['html'];
// Initialize fragment and query variables.
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
if (isset($link['href'])) {
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
}
else if ($link['title']) {
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (!$html) {
$link['title'] = check_plain($link['title']);
}
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
}
$output .= "</li>\n";
}
$i++;
}
$output .= '</ul>';
}
return $output;
}
?>If you compare this function with the original one, you will notice that it introduces two extra parameters:
- $start - first link to be displayed
- $end - last link to be displayed
Now, whenever you want to display your links you call this function. You need to include these extra parameters if you want to display only certain links. If you do not include these parameters, it will display all the links.
So, for your first block:
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'), 1, 5);
}
return $output;
?>and for the second block:
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'), 6);
}
return $output;
?>To split the upper level Primary Links into as many blocks as you'd like, you can override the theme_links function:
(drupal 5.1) In template.php:
<?php
function phptemplate_links($links, $attributes = array('class' => 'links'), $start = 1, $end = 0) {
$output = '';
if (count($links) > 0) {
if ($end==0) $end = count ($links);
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
if (($i>=$start) && ($i<=$end)) {
$class = '';
// Automatically add a class to each link and also to each LI
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' ' . $key;
$class = $key;
}
else {
$link['attributes']['class'] = $key;
$class = $key;
}
// Add first and last classes to the list of links to help out themers.
$extra_class = '';
if ($i == 1) {
$extra_class .= 'first ';
}
if ($i == $num_links) {
$extra_class .= 'last ';
}
$output .= '<li class="'. $extra_class . $class .'">';
// Is the title HTML?
$html = isset($link['html']) && $link['html'];
// Initialize fragment and query variables.
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
if (isset($link['href'])) {
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
}
else if ($link['title']) {
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (!$html) {
$link['title'] = check_plain($link['title']);
}
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
}
$output .= "</li>\n";
}
$i++;
}
$output .= '</ul>';
}
return $output;
}
?>If you compare this function with the original one, you will notice that it introduces two extra parameters:
- $start - first link to be displayed
- $end - last link to be displayed
Now, whenever you want to display your links you call this function. You need to include these extra parameters if you want to display only certain links. If you do not include these parameters, it will display all the links.
So, for your first block:
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'), 1, 5);
}
return $output;
?>and for the second block:
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'), 6);
}
return $output;
?>