Submitted by Nik on
Quite frequently I’ve been asked about putting images into site “sections”, depending on path or menu trail. Look up, that “Blog” image is what I’m talking about. It’s on all blog related pages. So, here goes – it’s nice to be able to finally offer this information here.
The first main chunk of code attempts to get a menu item and build an image link from that. The second chunk assumes failure of the first and tries again using a partial path method.
If all nodes on your site have menu entries, you can use that piece of code independently. Likewise, if all your nodes can be identified by the first bit of the path, the second chunk will stand alone.
I have got a mixture of the two on this site. A lot of the entries have menu entries, but the blog and portfolio section do not. Therefore, the image links on those sections are powered by the second chunk.
Note: this code expects to find sites/default/files of the GIF type in a directory ‘images/sections’ within my own theme directory. It also will only pick up sites/default/files that have names which are all lower case. In the case of menu entries that contain spaces, those will be replaced with hyphens, so if the menu link is “Site Map”, the image name will have to be “site-map.gif”. Path-based is really dependant on how you are using aliases (e.g. your pathauto.module setup) and isn’t really inside the scope of this article. You’ll have to figure that out yourself.
Okay; in order to not crowd up _phptemplate_variables(), I add just this one line of code in template.php inside that function (under ‘page’ – see here for details):
<?php
$vars['section_link'] = get_section_link();
?>
Then, elsewhere in that file, this code:
<?php
function get_section_link() {
// MENU - attempt to make a section link from a menu item, for this page
// get active menu trail into an array
$menu_items = _menu_get_active_trail();
// $menu_items[1] is the top parent of our menu container, e.g. primary links
// this gets the required menu item into an array
$link_array = menu_item_link($menu_items[1], FALSE);
// whip out spaces and make the name lower case
$section_name = strtolower($link_array['title']);
$section_name = str_replace(' ', '-', $section_name);
if ($section_link = render_link($section_name)) {
return $section_link;
}
// PATH - if we've not returned, we couldn't make a valid link from menu
// let's try a path approach instead?
if (module_exists('path')) { // dependency for drupal_get_path_alias
$sections = array(); // an empty array to collect stuff in
// get all the top level links in the primary nav (id of 2) into a array
$primary_nav = menu_primary_links(1, 2);
// iterate over the array and pull out the top level paths
foreach ($primary_nav as $menu) {
// get the first element of the aliased path for this menu item
$path_element = explode('/', drupal_get_path_alias($menu['href']));
// put the first chunk of each path onto an array
$sections[] = $path_element[0];
}
// get the aliased path for the page we're on
$section = explode('/', drupal_get_path_alias($_GET['q']));
$section_name = $section[0];
// if the path matches a nav item, create a section image
if (in_array($section_name, $sections)) {
if ($section_link = render_link($section_name)) {
return $section_link;
}
}
}
}
function render_link($section_name) {
// construct the image's path (mine are GIFs stored in a subdir of my theme)
$image_path = path_to_theme() . '/images/sections/' . $section_name . '.gif';
// make some text for the image's alt & title tags (SEO, accessibility)
$image_alt = $section_name . t( ' section');
$image_title = $section_name . t( ' section link');
// render image html using theme_image (returns NULL if file doesn't exist)
$section_image = theme('image', $image_path, $image_alt, $image_title);
// if the image rendered ok, render link using above variables
return ($section_image) ? l($section_image, $link_array['href'],
array('title' => $image_title), NULL, NULL, FALSE, TRUE) : NULL;
}
?>
Then finally in page.tpl.php (and any other page templates) we can use the variable in the “Drupal Way”, and print our variable where we like!
<?php if ($section_link): ?>
<div id="sectionTitle">
<?php print $section_link; ?>
</div>
<?php endif; ?>