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

FAPI and Zebra Striping

In:
  • Drupal 6
  • Drupal Recipes
28Feb2009

I recently had to add "zebra striping" to a very long form generated with Drupal's Form API (FAPI). Fortunately, FAPI already wraps each element in a div with class "form-item", so I just needed to add a second class to each item that alternates between "odd" and "even". The easiest method to do this is simply implement a theme_form_element() theme hook.

Below is my implementation of the this hook in the "exampletheme" theme. To use this, simply rename to "yourthemename_form_element()" and put it into your theme's template.php file.

<?php
/**
* Implement theme_form_element().
*
* Here we are adding "zebra striping" to FAPI form elements.
*/
function exampletheme_form_element($element, $value) {
 
// NRD: The following three lines allow us to add zebra striping.
 
static $count = 0;
 
$count++; 
 
$zebra = ($count % 2) ? 'odd' : 'even';
 
 
// This is also used in the installer, pre-database setup.
 
$t = get_t();

 
$output = '<div class="form-item '.$zebra.'"'; // NRD: Add zebra striping.
 
if (!empty($element['#id'])) {
   
$output .= ' id="'. $element['#id'] .'-wrapper"';
  }
 
$output .= ">\n";
 
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';

  if (!empty(
$element['#title'])) {
   
$title = $element['#title'];
    if (!empty(
$element['#id'])) {
     
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
    else {
     
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
  }

 
$output .= " $value\n";

  if (!empty(
$element['#description'])) {
   
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
  }

 
$output .= "</div>\n";

  return
$output;
}
?>

To style this content, add something like the following to your theme's CSS:

/* Add zebra-striping to form items.  */
div.fieldset-wrapper div.odd {
  background-color: #eee;
  padding: 2px 0 2px 0;
}

  • Eric Weik's blog
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 6 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
  • Sending Multiple Customized Confirmation Messages 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