Adding website section images in Drupal themes

<?php
$vars
['section_link'] = get_section_link();
?>

<?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;
}
?>

<?php if ($section_link): ?>
  <div id="sectionTitle">
    <?php print $section_link; ?>
  </div>
<?php endif; ?>

Blog Tags: DrupalThemesPHP

Comments

Could this be used for css files?

The reason i like this is that the site i am designing has about 5 sections. Each section has a different header image. The person who will be maintaining the site needs a simple way to select which section the pages they make will appear in, thus having the correct header.

A drop down would be perfect =P but just having them type a specific path in the pathauto field will work in a pinch.

Is there not a module that will allow you to create separate css files based on primary links?

I suppose i could use a block but how would the person maintaining the site be able to designate which block layout to use for a new page?

Thanks for this. Should this still be viable in drupal 6.x?

This is very interesting. I'm a newbie in drupal, so it can be very helpful to me.

Yeah, or through the Drupal admin (using the blocks module) you could set your headline images for each section by adding blocks to the content area and specifying "show only on these page" "/blog*" or whatever. That's how I did it on my portfolio website, that I built with Drupal.

nice article. Thanks.

This is *exactly* what I'm trying to do - thanks for much for the clear walkthrough, I'm very glad to be able to grab contextual section information without resorting to a third-party module or taxonomy.

Ok.
In other words there is no easy way of defining a main banner per section ?
Steve's solution is ok, but as for me slightly to complicate workaround. It is ok and it will work, but I thin it is not the best if you ever have to explain admin user how things work.
Has anyone tried this one ?
http://drupal.org/project/themekey

It is really great having opportunity for reading this great information’s. I like reading it because it is brief and step by step well explained, so everyone can understand it. It is really great tips for everyone, Thank You Drupal is really great.

Is there anyone who tried it out , can anyone confirm that all this works properly.

I do it the same way as Steve T but it's nice to know there are other possibilities. Thank you.

This is *exactly* what I'm trying to do - thanks for much for the clear walkthrough, I'm very glad to be able to grab contextual section information without resorting to a third-party module or taxonomy.

Add new comment