Running Drupal 6.11 from cvs, theme version 6.x-2.4.

Getting "undefined variable" php warning on the $admin variable called in line 25 of template.php. Showed up on most site pages, whether logged in or not, and oddly, whether in admin or not.

The variable is defined within an if statement beginning on line 10 of template.php; however, unless that statement is true, the warning shows. Current code in template.php starting Line 9:

function phptemplate_body_class($left, $right) {
  if (arg(0) == 'admin') {
    $admin = ' admin';
  }
  if ($left != '' && $right != '') {
    $class = 'sidebars';
  }
  else {
    if ($left != '') {
      $class = 'sidebar-left';
    }
    if ($right != '') {
      $class = 'sidebar-right';
    }
	}
  if (isset($class)) {
    print ' class="'. $class . $admin .'"';
  }
}

I apparently fixed it, maybe awkwardly, by adding an else statement below line 11 that defines the admin variable as an empty string. Code with my, um, fix:

function phptemplate_body_class($left, $right) {
  if (arg(0) == 'admin') {
    $admin = ' admin';
  } else {
    $admin = '';
  }
  if ($left != '' && $right != '') {
    $class = 'sidebars';
  }
  else {
    if ($left != '') {
      $class = 'sidebar-left';
    }
    if ($right != '') {
      $class = 'sidebar-right';
    }
	}
  if (isset($class)) {
    print ' class="'. $class . $admin .'"';
  }
}

I'm sure you will find a more elegant and permanent fix, but just wanted to alert you to this. Let me know if you need more info.

Comments

andregriffin’s picture

Interesting. Do you think this could because 6.11 may have bugs, since it's not release yet, or really a Framework bug? I'd like to see the changelog for 6.11 to see if it mentions anything about $admin.

jaysmall’s picture

Certainly possible. Seems strange that the $admin variable would be left hanging undefined. Now that 6.11 is an official release, we can recheck.

dazweeja’s picture

It's obvious from the code snippet that it's a Framework bug. The $admin variable is called on line 25 but unless it's set on line 11 (if arg(0) == 'admin'), the variable is undefined. The only way it could be a Drupal bug was if it was passed in as a parameter or called as a global variable.

Jay's code will fix the problem or, better still, initialize the variable:

$admin = '';
if (arg(0) == 'admin') {
$admin = ' admin';
}

Or use a ternary (shorter but I think harder to read):

$admin = (arg(0) == 'admin') ? ' admin' : '';

dazweeja’s picture

I just saw this comment in another issue:

http://drupal.org/node/444538#comment-1569090

You could use something like this so the body tag gets the admin class even when there are no sidebars:

function phptemplate_body_class($left, $right) {
$class = array();
if ($left != '' && $right != '') {
$class[] = 'sidebars';
}
else {
if ($left != '') {
$class[] = 'sidebar-left';
}
if ($right != '') {
$class[] = 'sidebar-right';
}
}

if (arg(0) == 'admin') {
$class[] = 'admin';
}
if ($class) {
print ' class="'. implode(' ', $class) . '"';
}
}

andregriffin’s picture

Hm. Yeah, that seems to be doing good things for me here. I'm no PHP expert by any means, would you call that last function all good and ready to be included in the next Framework release?

dazweeja’s picture

I think so but I'd probably tidy it up to:

function phptemplate_body_class($left, $right) {
	$class = array();

	if ($left != '' && $right != '') {
		$class[] = 'sidebars';
	}
	elseif ($left != '') {
		$class[] = 'sidebar-left';
	}
	elseif ($right != '') {
		$class[] = 'sidebar-right';
	}

	if (arg(0) == 'admin') {
		$class[] = 'admin';
	}

	if ($class) {
		print ' class="' . implode(' ', $class) . '"';
	}
}
andregriffin’s picture

Ok, it will be included in 2.5. Seems to be working well.

andregriffin’s picture

Assigned: Unassigned » andregriffin
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.