Skip to main content
  • Home
  • Work
  • Photography
  • Contact
  • Client Extranet
New Rivers Digital
Home Blogs Eric Weik's blog

Use THEME_preprocess_page() to add a "subsection" body class.

In:
  • Drupal Recipes
  • Theming
  • Zen
  • Zen Theming
1Sep2009

The Zen Theme for Drupal is a great place to start building a standards-compliant Drupal site. One of the useful features of Zen for site design is the fact that it adds multiple classes to the document body (in addition to the classes already added by Drupal core). But if these classes are not enough, we can easily add more through THEME_preprocess_page().

By default, Zen uses the URL path to create "section" and "page" classes which are added as to the body tag. Take for example, the hypothetical page: site.com/spectrum/red the body tag would include the classes:

  • page-spectrum-red
  • section-spectrum

In most cases this is sufficient to craft CSS selectors to style a specific page, or collection of pages within a section. However, what about the case where content is buried deeper? For example, imagine these urls:

  • site.com/spectrum/visible/orange
  • site.com/spectrum/visible/yellow
  • site.com/spectrum/ultraviolet/90nm

It would be nice to be able to able to add a selector to style all of the pages in the "visible" subsection differently than those in the "ultraviolet" subsection. Lets imagine that for site.com/spectrum/visible/orange, the the following selectors were available to us:

  • page-spectrum-orange
  • section-spectrum
  • subsection-visible

We can do this by implementing hook_preprocess_page() in template.php, with the following (where THEME is the id of our theme):

<?php
/**
* Override or insert variables into the page templates.
*
* NRD: Output body a body class based on the "subsection" which works
*      much like the default Zen "section" body class. 
*      E.g.: /section0/subsection1/page2 =>
*            section-section0
*            subsection-subsection1
*
* @param $vars
*   An array of variables to pass to the theme template.
* @param $hook
*   The name of the template being rendered ("page" in this case.)
*/
function zen_THEME_preprocess_page(&$vars, $hook) {
 
// Add unique class for each subsection (e.g. /section0/subsection1/page2
 
$path = drupal_get_path_alias($_GET['q']);
 
$sections = explode('/', $path,3);
 
 
// If subsection *and* page are defined
 
if ($sections[1] && $sections[2]) {
   
$classes[] = zen_id_safe('subsection-' . $sections[1]);
  }

 

// Add classes to body 
 
$vars['body_classes_array'] = $classes;
 
$vars['body_classes'] = implode(' ', $classes); // Concatenate with spaces.

}
?>

Now we can select and style all pages in the "visible" subsection with a CSS selector such as:

body.subsection-visible #somecontainer {
  background-color: #f0f0f0;
}
  • Eric Weik's blog

Comments

#1 Without Zen?

Andre M.'s picture

Submitted by Andre M. (not verified) on Wed, 03/24/2010 - 10:05.

Since I developed my own theme, is it possible to do this without the use of zen theme?

  • reply

#2 Zen not required...

Eric Weik's picture

Submitted by Eric Weik on Wed, 03/24/2010 - 11:24.

Hi Andre,

Yes, you can implement this in any theme. Add the
function THEME_preprocess_page(&$vars, $hook) {} code from my example to your template.php (replacing THEME with your theme name). It calls a Zen theme function called zen_id_safe(), so you'll need to implement this in your theme as provided by the Zen theme (it just makes sure that the id output conforms to W3C specifications for ID attributes).

If I were you, I would download the Zen theme and open up zen/zen/template.php, and simply copy and paste the zen_id_safe function declaration into your theme's template.php. While it is open, poke around and see how the Zen theme does things. Even when I am doing theme development "from scratch", I often pull in parts of the Zen theme as recipes. It does a number of useful things.

Or if you want, you can just copy and paste from here:

<?php
/**
* Converts a string to a suitable html ID attribute.
*
* <a href="http://www.w3.org/TR/html4/struct/global.html#h-7.5.2" title="http://www.w3.org/TR/html4/struct/global.html#h-7.5.2">http://www.w3.org/TR/html4/struct/global.html#h-7.5.2</a> specifies what makes a
* valid ID attribute in HTML. This function:
*
* - Ensure an ID starts with an alpha character by optionally adding an 'id'.
* - Replaces any character except alphanumeric characters with dashes.
* - Converts entire string to lowercase.
*
* @param $string
*   The string
* @return
*   The converted string
*/
function zen_id_safe($string) {
 
// Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
 
return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
}
?>

I hope this helps! -Eric

  • reply

#3 The urls

Andre M.'s picture

Submitted by Andre M. (not verified) on Fri, 03/26/2010 - 14:34.

Hi Eric

Thanks for replying... How unfortunate I cannot test this, since the server I was working on is down at the moment... :( As soon as it becomes online I will try and post here.

Thank you,
Andre M.

  • reply

#4 Some problem

Andre M.'s picture

Submitted by Andre M. (not verified) on Mon, 03/29/2010 - 11:05.

Hi Eric

I put the mytheme_id_safe function first. Then, below it, I put the mytheme_preprocess_page function. I also added the following code into page.tpl.php:

<?php echo $body_classes; ?>

However, Drupal didn't add any class or id (with subsection-) in body. Also, in admin page, I have the following warning:

warning: implode() [function.implode]: Bad arguments. in /path-to/mytheme/template.php on line 457.

Referring to the following line:

$vars['body_classes'] = implode(' ', $classes);

I must have done something wrong...
Thanks,
Andre

  • reply

#5 body_classes

Susheel Chandradhas's picture

Submitted by Susheel Chandradhas (not verified) on Mon, 12/27/2010 - 12:17.

Andre,

I saw somewhere that body_classes and body_classes_array have been changed to classes and respectively. If you make the change, you should see that this code does indeed work.

However, I'd like the class names to be present in addition to the regular classes that zen inserts. is there a way to do so?

  • reply

Post new comment

Warning
I strongly encourage and welcome links and feedback. However, this site is moderated and comments with inappropriate links are rejected. Please do not post a one-line "Me too" or "Great post!" comment just so you can link to your site. Thank you for your understanding.
The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Use to create page breaks.

More information about formatting options

Blog Posts (RSS)

About

Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.

Contact Details

New Rivers Digital
PO Box 784

Lancaster, VA 22503

Voice+1-804-577-8526
Fax +1-804-462-3229
Contact Form

Content Tags

Abstract B&W Celestial Clouds Drupal 6 Drupal Recipes Drupal Sites Estuary Etsy Government Grasses HDR Long Exposure Macro New Rivers Digital Orton Photoblog Photo Expedition Photoset Renderblog RGB Sketchbook Snow Storm Structure Synth Sunflow Sunset Theming Webform.module Wide Angle Zen Zen Theming
more tags

Recent comments

  • This is stunning
    2 weeks 5 days ago
  • #42 worked well for me
    3 weeks 3 days ago
  • Email by country
    12 weeks 3 days ago
  • hid conversion kit
    13 weeks 3 days ago
  • James
    14 weeks 2 days ago
  • Michelle
    14 weeks 3 days ago

Popular content

Today's:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Non-Unique Conditional Email Addresses with Webform.Module

All time:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Sending Multiple Customized Confirmation Messages with Webform.Module
  • Non-Unique Conditional Email Addresses with Webform.Module

Activity Stream

  • Sun, 01/29/2012 - 22:31

  • Flickr Eric posted #0228 - Sunset Tree 10:31pm #
  • Flickr Eric posted #0225 - Rappahannock 10:30pm #
  • Flickr Eric posted #5617 - Hobie Sailing 11:56am #
  • Mon, 01/16/2012 - 10:43

  • Flickr Eric posted #5687 - Broken Tree (Digital Sketch) 10:43am #
  • Fri, 01/06/2012 - 07:54

  • Flickr Eric posted #8975 - Greenvale Creek 7:54am #
  • Flickr Eric posted #8824 - Grass (blue ch) 7:46am #
  • Flickr Eric posted #5687 - Broken Tree (IR) 7:35am #
  • Flickr Eric posted #3205 - Toadstool 7:21am #
  • Flickr Eric posted #2318 - Tree and Beach 7:13am #
  • Flickr Eric posted #2251 - Low Tide Arrival 7:04am #
more from my activity-stream


I am a member of the Drupal Association.
Eric At NRD on Drupal.org
Circumjacence (Eric Weik) on Twitter
Circumjacence on Delicious
Eric Weik on Linkedin
Circumjacence (Eric Weik) on Flickr
Circumjacence (Eric Weik) at StumbleUpon

Powered by Drupal & Genesis | Valid XHTML 1.0 Strict | Syndicate content RSS Feed

© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form