description

This snippet allows you to override the site logo and site name for sections of your site, based on paths.

Drupal 5 Implementation

As an illustrative example, the following step-by-step approach shows you how to switch the site logo and site name for when users are viewing various sections (based on path)

Step 1 of 1

  1. Using a text editor like notepad.exe or equivalent, paste the snippet below into your template.php file.
  2. Edit the path arguments to suit your needs
  3. Upload your edited template.php file to your active theme folder and your new layouts will take effect automatically
<?php
/**
* This snippet overrides the logo and site name when users
* are viewing certain sections of your site.*/

function _phptemplate_variables($hook, $variables = array()) {
  switch ($hook) {
    case 'page':
      if ((arg(0) == 'blog')) {// changes the logo and site name when viewing blogs
        $variables['site_name'] = 'blog section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'image')) { // changes the logo and site name when viewing images
        $variables['site_name'] = 'user section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'admin')) { // changes the logo and site name when viewing admin pages
        $variables['site_name'] = 'admin section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'product')) { // changes the logo and site name when viewing product pages
        $variables['site_name'] = 'admin section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      break;
  }

  return $variables;
}
?>

Loading custom layout files for each section as well as changing the site name and logo

If you also want to load a page-customtpl.php layout file for each section, you can add the line $variables['template_file'] = 'page-custom';.

Here's an example of the earlier snippet that includes a custom page layout option.

/**
* This snippet loads a custom layout file and 
* overrides the logo and site name when users
* are viewing certain sections of your site
* based on path.
*/

function _phptemplate_variables($hook, $variables = array()) {
  switch ($hook) {
    case 'page':
      if ((arg(0) == 'blog')) {
        $variables['template_file'] = 'page-blog'; // load custom page-blog.tpl.php layout file
        $variables['site_name'] = 'blog section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'image')) {
       $variables['template_file'] = 'page-image'; // load custom page-image.tpl.php layout file
        $variables['site_name'] = 'user section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'admin')) { 
        $variables['template_file'] = 'page-admin'; // load custom page-admin.tpl.php layout file
        $variables['site_name'] = 'admin section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      if ((arg(0) == 'product')) {
        $variables['template_file'] = 'page-product'; // load custom page-product.tpl.php layout file
        $variables['site_name'] = 'product section name';  // change the site name
        $variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      break;
  }

  return $variables;
}

More example path arguments you can use

Here are more examples of using the path that you can use to extend out your layouts even further.

arg(0)=="admin"// is /admin

arg(0) =="node"// is /node

arg(0)=="user" // is /user

arg(0)=="node" && arg(1)=="add" // is /node/add

arg(0)=="node" && arg(arg(2)=="edit" // is /node/###/edit

arg(0)=="user" && arg(1)=="add" // is /user/add

arg(0)=="admin" && arg(1)="user" && arg(2)=="create" // is /admin/user/create

arg(0)=="alias" && arg(1)=="alias1" is /alias/alias1

arg(0)=="taxonomy" && arg(1)=="term" && arg(2)=="term#" // is /taxonomy/term/term#

//arg(1)=="comment"

//arg(2)=="reply"

Drupal 6 Implementation

The implementation of overriding theme variables changed in Drupal 6 with the introduction of preprocess functions and templates. Below is an example of code that would accomplish the same purpose in Drupal 6. The function should go into your theme's template.php and you will need to flush all cache (really just the theme cache) to get Drupal to pick up and display the new code.

/**
 * This section sets up different site name and slogan information based on path
 */
/**
* This snippet overrides the logo and site name when users
* are viewing certain sections of your site.*/

function themename_preprocess(&$variables, $hook) {
        if ((arg(0) == 'firstpath')) {// changes the logo and site name when viewing blogs
        $variables['site_name'] = 'firstpath_site_name';  // change the site name
		$variables['site_slogan'] = 'firstpath_site_slogan';  // change the site slogan
        //$variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      elseif ((arg(0) == 'secondpath')) { // changes the logo and site name when viewing images
        $variables['site_name'] = 'secondpathsitename';  // change the site name
		$variables['site_slogan'] = 'secondpathsiteslogan';  // change the site slogan
        //$variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
      }
      elseif ((arg(0) == 'thirdpath')) { // changes the logo and site name when viewing admin pages
        $variables['site_name'] = 'thirdpathsitename';  // change the site name
		$variables['site_slogan'] = 'thirdpathsiteslogan';  // change the site slogan
        //$variables['logo'] = '/path/to/newlogo/logo.png'; // change the site logo
  }}

Comments

lordrt21’s picture

I tried the code for drupal 6, but there is no change in the logo when I switch nodes... I replaced the themename by my own and added the path according to mine but I get only the logo I defined as default in my theme from the theme's settings page, and if I remove that setting then no logo at all is displayed, irrespective of the code above.
Does this code only work for sections, like admin/blog/page, and cannot be applied on nodes themselves, e.g. node/1, node/50 etc?

kccmcck’s picture

This code should work based purely on path. To switch logos based on node/1 or node/50 as you referenced above, you would need to target arg(1).

I am wondering if this works for aliased paths, however. In my case, this technique works for node/1 or node/50... but if node/50 is aliased to about/history and I try to switch logos based on arg(0) == 'about' the switch does not work.

Ahh, this is brilliant: http://drupal.org/node/838508

Brilliant. Worked like a charm for using this technique with aliased URLs.

mauryg’s picture

Having same problem as lordrt21. D6 w/ Garland theme. Added following to template.php:

function garland_preprocess(&$vars, $hook) {
        if ((arg(0) == 'biysig')) {
	$variables['site_name'] = 'Build It Yourself SIG';  // change the site name
        $variables['logo'] = 'images/logos/biysig_75.png'; // change the site logo
      }
      elseif ((arg(0) == 'websig')) { 
        $variables['site_name'] = 'Web Design SIG';  // change the site name
        $variables['logo'] = '/files/images/logos/websig_75.png'; // change the site logo
      }
      elseif ((arg(0) == 'winsig')) { 
        $variables['site_name'] = 'Windows Software SIG';  // change the site name
        $variables['logo'] = '/files/images/logos/winsig_75.png'; // change the site logo
  }
}

Neither name nor logo changed.
Tried changing $variables to $vars per D6. Still no go
Created block using snippet from http://drupal.org/node/838508. Able to display arguments.
What am I missing???

lordrt21’s picture

This module could act as an alternative: http://drupal.org/project/logotool, used it in one site, and very satisfied customer feedback

mauryg’s picture

@lordrt21
thank you. Logotool was indeed the answer I was looking for. I incorporated both the 'logo' and the site section name into the logo image. In fact, pobster even explained to me how I could get each of the separate section logos to point to a different section 'home' page when the user clicked on the logo. http://drupal.org/node/1114374. (Now that I think about it, might even be able to use the logotool 'templating' concept to change the site name for each section.)

mstrelan’s picture

Please let me know if you get this working for site_name