Hello everybody, I'm not sure if this is the correct place, but here it goes. I'm trying to output a body ID for each page/survey/image gallery, this way I can control which menu items and elements are highlighted depending on where the user currently is, all controlled via CSS.

How can I determine what the machine readable name (or unique name) is for each page/survey/image gallery/view. Any ideas would be really helpful. I am trying to accomplish this without parsing the URL.

Comments

nevets’s picture

Borrowed from arg(), somewhere toward the start of your page.tpl.php file add

<?ph
  if ($_GET['q']) {
    $body_id = str_replace('/', '-', $_GET['q']);
  }
   else {
     $body_id = "main";
   }
?>

Then where you have you body tag you need something like

<body id="<?php print $body_id; ?>">
Ian Burge’s picture

Thanks for the reply, I ended up using a combo of the arg() and server request. It may not be the most ideal way but it seems to work well with clean urls enabled.

This is how I have done it.

function getPageId() {

if (arg(0)== "node") {

//this is a drupal node
global $base_path;
list(,$path) = explode($base_path, $_SERVER['REQUEST_URI'], 2);
list($path,) = explode('?', $path, 2);
$path = rtrim($path, '/');

// Construct the id name from the path, replacing slashes with dashes.
$pageId = "pgName-".str_replace('/', '-', $path);

}else {
//image gallery, user login
$pageId = "pgName-".arg(0);

}

//return for output in page body tag
return $pageId;
}