Skip to content

Instantly share code, notes, and snippets.

@oliverguenther
Created February 10, 2012 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliverguenther/1784525 to your computer and use it in GitHub Desktop.
Save oliverguenther/1784525 to your computer and use it in GitHub Desktop.
Drupal 7 Snippet: Select a header image based on the topmost active menu item
/**
* Return header image based on the node id of the topmost active menu item
* @return Absolute path to the image selected
*/
function get_header_image() {
// Get the Page's Parent Menu Item
$menuParent = menu_get_active_trail();
// background images based on node id's
$node_imgs = array(
8 => 'header1.jpg',
12 => 'header2.jpg',
25 => 'header3.jpg',
);
$header = 'default_header.jpg';
// Get the topmost menu element
if (!empty($menuParent[1])) {
$linkpath = $menuParent[1]['link_path'];
if (!empty($linkpath)) {
// search for node id in it's link_path
preg_match("/node\/(\d+)/i", $linkpath, $match);
if (!empty($match) && !empty($node_imgs[$match[1]]))
$header = $node_imgs[$match[1]];
}
}
// return absolute path to the
return base_path() . drupal_get_path('theme', 'MY_THEME_NAME') . '/images/header/' . $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment