learning to be human, since 1984
Archive for Thursday 18 February 2010
Returning an Alias from primary_links’s links['href'] – or – How I became a Drupal Wizard
Feb 18th
I’m building a custom template in Drupal for the farm, and needed a way to spit out a custom implementation of primary_links navigation unordered list. I needed something like this:
<ul id="nav"> <li class="first"><a href="link-goes-here" title="wow, how descriptive"><span>link 1 copy</span></a></li> <li><a href="link-goes-here2" title="round 2, FIGHT!"><span>link 2 copy</span></a></li> </ul>
The spans inside the anchor tag are valid xhtml strict, and necessary for the nested/floating backgrounds. The l() function in drupal does a fine job of spitting out valid code, but I also haven’t found anything to delimit the angle brackets in the span element.
Here’s how I solved the problem.
First, I added a preprocess function in template.php and copied this chunk of code from root/includes/theme.inc:
function theme_links($links, $attributes = array('class' => 'links')) {
global $language;
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = $key;
// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
&& (empty($link['language']) || $link['language']->language == $language->language)) {
$class .= ' active';
}
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
if (isset($link['href'])) {
// Pass in $link as $options, they share the same keys.
$output .= l($link['title'], $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
I then modified the code as follows:
function stoneyacresfarm_links($links, $attributes = array('class' => 'links')) {
global $language;
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = $key;
// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
&& (empty($link['language']) || $link['language']->language == $language->language)) {
$class .= ' active';
}
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
if (isset($link['href'])) {
// Pass in $link as $options, they share the same keys.
// here are the modifications
$temphref = drupal_lookup_path('alias', $link['href'], '');
$output .= '<a href="' . $temphref . '" title="' . $link['title'] . '"> <span>' . $link['title'] . '</span></a>';
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
Lines 29 & 30 are the brilliant bits. I scoured api.drupal.org till I found theĀ drupal_lookup_path function. Follow the link for the arguments that can be passed in. I specified that I wanted an alias, gave it the path ( links['href'] ), and passed in a blank character for language.
This is all rendered out in my page-front.tpl.php and page.tpl.php files in the standard fashion:
<?php if (!empty($primary_links)): ?>
<?php print theme('links', $primary_links, array('id' => 'nav')); ?>
<?php endif; ?>
I hope this helps!