Created
January 21, 2011 23:13
-
-
Save inlikealion/790617 to your computer and use it in GitHub Desktop.
Body classes for almost everything in Drupal 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function phptemplate_preprocess_page(&$vars, $hook) { | |
// Classes for body element. Allows advanced theming based on context | |
// (home page, node of certain type, etc.) | |
$body_classes = array($vars['body_classes']); | |
if (!$vars['is_front']) { | |
// Add unique classes for each page and website section | |
$path = drupal_get_path_alias($_GET['q']); | |
list($section, ) = explode('/', $path, 2); | |
$body_classes[] = mytheme_id_safe('page-' . $path); | |
$body_classes[] = mytheme_id_safe('section-' . $section); | |
if (arg(0) == 'node') { | |
if (arg(1) == 'add') { | |
if ($section == 'node') { | |
array_pop($body_classes); // Remove 'section-node' | |
} | |
$body_classes[] = 'section-node-add'; // Add 'section-node-add' | |
} | |
elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) { | |
if ($section == 'node') { | |
array_pop($body_classes); // Remove 'section-node' | |
} | |
$body_classes[] = 'section-node-' . arg(2); // Add 'section-node-edit' or 'section-node-delete' | |
} | |
} | |
} | |
$vars['body_classes'] = implode(' ', $body_classes); // Concatenate with spaces | |
} | |
/** | |
* Converts a string to a suitable html ID attribute. | |
* | |
* - Preceeds initial numeric with 'n' character. | |
* - Replaces any character except A-Z, numbers, and underscores with dashes. | |
* - Converts entire string to lowercase. | |
* - Works for classes too! | |
* | |
* @param $string | |
* The string | |
* @return | |
* The converted string | |
*/ | |
function mytheme_id_safe($string) { | |
if (is_numeric($string{0})) { | |
// If the first character is numeric, add 'n' in front | |
$string = 'n'. $string; | |
} | |
return strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string)); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified from the Zen base theme, as found in the comments at http://mydrupalblog.lhmdesign.com/classes-almost-everything-drupal-theme