Omega doesn't append the body class with a sidebar-position, no-sidebar class? Am I missing something, or is there an option I need to set. Please enlighten

Comments

JSCSJSCS’s picture

Not sure what you are asking...

johncionci’s picture

Priority: Critical » Normal
StatusFileSize
new71.62 KB

I believe what Jumoke is saying is that even though I have sidebars "active" the body class still shows the class "no-sidebars".
(See Attached)

finex’s picture

I'm not able to reproduce with an HTML5 Omega 7.x-3.0 subtheme (responsive settings enabled). I've one sidebar and the body classes doesn't show anything related to sidebars.

jumoke’s picture

FineX, yes--just same problem I am having. I am using the HTML5 Omega and with or without sidebars, the sidebar classes do not print to my body class. I even created a preprocess function to stick the sidebar classes into my bodyclass - no response! Omega just ignores it.

I need to access my sidebar class in my body class! If anyone has any luck pls let me know. Thanks.

jumoke’s picture

Hey John :)
I don't even get no-sidebars printed at all. With or with no sidebars -- i get nothing

finex’s picture

Alpha template.php removes those classes:

/**
 * Implements hook_preprocess_html().
 */
function alpha_alpha_preprocess_html(&$vars) {
  $theme = alpha_get_theme();

  foreach (array('two-sidebars', 'one-sidebar sidebar-first', 'one-sidebar sidebar-second', 'no-sidebars') as $exclude) {
    if ($index = array_search($exclude, $vars['attributes_array']['class'])) {
      unset($vars['attributes_array']['class'][$index]);
    }
  }

The question is: why this behaviour?

johncionci’s picture

It seems like this has always been the way for Omega. If I go to inspect any of their showcase sites they all have the same issue. Going through the preprocess_page functions Omega seems to reference building its body classes like ZEN, but and seems like its missing a whole section of code that Zen has.

Omega

// ZEN - BODY CLASSES
// Classes for body element. Allows advanced theming based on context
// (home page, node of certain type, etc.)
$classes = split(' ', $vars['body_classes']);
// Remove the mostly useless page-ARG0 class.
if ($index = array_search(preg_replace('![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s', '', 'page-'. drupal_strtolower(arg(0))), $classes)) {
  unset($classes[$index]);
}
if (!$vars['is_front']) {
  // Add unique class for each page.
  $path = drupal_get_path_alias($_GET['q']);
  $classes[] = omega_id_safe('page-' . $path);
  // Add unique class for each website section.
  list($section, ) = explode('/', $path, 2);
  if (arg(0) == 'node') {
    if (arg(1) == 'add') {
      $section = 'node-add';
    }
    elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
      $section = 'node-' . arg(2);
    }
  }
  $classes[] = omega_id_safe('section-' . $section);
}
$vars['body_classes_array'] = $classes;
$vars['body_classes'] = implode(' ', $classes); // Concatenate with spaces.

Zen


  // We need to re-do the $layout and body classes because
  // template_preprocess_page() assumes sidebars are named 'left' and 'right'.
  $vars['layout'] = 'none';
  if (!empty($vars['sidebar_first'])) {
    $vars['layout'] = 'first';
  }
  if (!empty($vars['sidebar_second'])) {
    $vars['layout'] = ($vars['layout'] == 'first') ? 'both' : 'second';
  }
  // If the layout is 'none', then template_preprocess_page() will already have
  // set a 'no-sidebars' class since it won't find a 'left' or 'right' sidebar.
  if ($vars['layout'] != 'none') {
    // Remove the incorrect 'no-sidebars' class.
    if ($index = array_search('no-sidebars', $vars['classes_array'])) {
      unset($vars['classes_array'][$index]);
    }
    // Set the proper layout body classes.
    if ($vars['layout'] == 'both') {
      $vars['classes_array'][] = 'two-sidebars';
    }
    else {
      $vars['classes_array'][] = 'one-sidebar';
      $vars['classes_array'][] = 'sidebar-' . $vars['layout'];
    }
  }

Maybe the answer lies in there...?

jumoke’s picture

Category: support » bug

Hi FineX, I noticed that too and commented that out of my alpha template.php -- and all it prints is no-sidebar whether sidebar exists or not.

JSCSJSCS’s picture

I am not able to reproduce this in my Omega HTML5 7.x-3.0 subtheme. I do not get any additional BODY classes associated with whether or not I have sidebars.

I am interested in knowing what people would do with those classes if they had them? I'm fairly new at this and can't see what I could do with those classes in the body tag or what problem it would solve to have them.

jumoke’s picture

This snippet by rlhawk: http://pastebin.com/jED1ZpDY doesn't work for me either. Nothing happens

function YOURTHEMENAME_alpha_preprocess_html(&$vars) {
  $sidebar_count = 0;
  $sidebars = array('sidebar_first', 'sidebar_second');
  $body_sidebar_classes = array();
  foreach ($sidebars as $sidebar) {
    if (isset($vars['page']['content']['content'][$sidebar])) {
      $sidebar_count ++;
      $body_sidebar_classes[] = str_replace('_', '-', $sidebar);
    }
  }
  switch ($sidebar_count) {
    case 0:
      $body_sidebar_classes[] = 'no-sidebars';
      break;
    case 1:
      $body_sidebar_classes[] = 'one-sidebar';
      break;
    case 2:
      $body_sidebar_classes[] = 'two-sidebars';
      break;
  }
  foreach ($body_sidebar_classes as $body_sidebar_class) {
    $vars['attributes_array']['class'][] = $body_sidebar_class;
  }
}
johncionci’s picture

@ JSCSJSCS - Not sure about everyone else but since I do mostly front end work they definitely aid me in applying certain CSS. For example if there are no sidebars you would want the main content area to be a full width, or if there are two sidebars it would shrink to fit them both.

jumoke’s picture

Hi JSCSJSCS,
You can do alot with the sidebar class printed in the bodyclass actually.
For example, I want to theme my title a certain way, e.g. to float completely left when a left sidebar exists for a certain content type. So in my case, I need the page-node-type class and sidebar class. Ya dig?

My other option was:
Extracting title declarations from my .tpl this way i would wrap it in an if statement to print title before sidebars and content. But i can't for the life of me figure this cos of the way omega separates things out between many tpl files.

JSCSJSCS’s picture

@johncionci - Omega Theme already does this width stuff (sizing) for me without needing any body classes. I could see its usefullness in other themes though.

JSCSJSCS’s picture

@Jumoke - I don't know enough about it to answer. It fascinates me though. The Omega theme is so flexable that every region can be set up to have "sidebars", as many or as little as you want in any order you want. "Sidebars" might be in the header, footer, content or other custom region. I might have three sidebars left then the content in the content region followed by a fourth sidebar. Since they can be anywhere and be of any number, why you would want to know this by using a class in the BODY tag (which engulfs everything in the page just about) is what I don't understand.

steveoliver’s picture

Title: Body class=sidebars, no-sidebars? » Omega intermediate preprocessing
Status: Active » Closed (works as designed)

From what I've encountered while trying to get a few simple body classes into my Omega subtheme, it appears that Omega does some "intermediate" preprocessing, meaning it puts variables in different places and has it's own preprocess hooks (_alpha/omega_ variations of THEMENAME_preprocess_*).

(See my recent g.d.o comment on "Adding code to preprocess-html.inc").

My current working solution for the latest Omega 3.x on D7 is:
[my_omega_html5_subtheme]/preprocess/preprocess-html.inc:]

/**
 * Preprocess HTML.
 *
 * Implements hook_alpha_preprocess_html().
 */
function THEMENAME_alpha_preprocess_html(&$variables) {
  // $variables['classes_array'][] = 'this-class-will-never-appear';
  $variables['attributes_array']['class'][] = 'this-class-will-appear';
}

The way that this ISN'T a standard THEME_preprocess_html() implementation is that it is not using the $variables['classes_array'][] array. It is using this Omega specific ['attributes_array']['class'] array.

I hope this helps.

Marking "closed: works as designed" even though it should be in a new category called "Read this because Omega is crazy complex".

star-szr’s picture

Tested the following with Drupal 7.12 and Omega 3.1 to add back the core sidebar classes.

YOURTHEMENAME/preprocess/preprocess-html.inc

function YOURTHEMENAME_alpha_preprocess_html(&$variables) {
  // Add information about the number of sidebars.
  if (!empty($variables['page']['content']['content']['sidebar_first']) && !empty($variables['page']['content']['content']['sidebar_second'])) {
    $variables['attributes_array']['class'][] = 'two-sidebars';
  }
  elseif (!empty($variables['page']['content']['content']['sidebar_first'])) {
    $variables['attributes_array']['class'][] = 'one-sidebar sidebar-first';
  }
  elseif (!empty($variables['page']['content']['content']['sidebar_second'])) {
    $variables['attributes_array']['class'][] = 'one-sidebar sidebar-second';
  }
  else {
    $variables['attributes_array']['class'][] = 'no-sidebars';
  }
}
sarahjean’s picture

The preprocess snippet in #16 worked for me. I realize that Alpha is stripping these out intentionally for some reason, but I prefer to have these classes available because there are often design elements in comps I am given that require adjustments based on different sidebar layouts.

rlhawk’s picture

This snippet continues to work well for me. The names of the regions will be specific to your theme, so the code may need to be altered. In the snippet, the sidebar regions are named "sidebar_first" and "sidebar_second", which are the defaults when using an Omega starter kit.

The snippet can either go in THEMENAME/preprocess/preprocess-html.inc or THEMENAME/template.php. In either case, be sure the opening <?php tag is there.

rlhawk’s picture

Sorry, I'm referring to my snippet referenced in comment #10 and available here: http://pastebin.com/jED1ZpDY.

letrotteur’s picture

#16 works just great! Thanks.

basvredeling’s picture

Indeed #16 works well.
I guess they removed it from omega / alpha because of html5 semantics or responsivity.

  • In case of responsive designs the logic is as follows: "what might be a sidebar on a desktop 1280*800, might not be a sidebar on your mobile device of 500px wide"
  • In case of html 5 semantics the logic might be (but I'm guessing here): "if you use html5 you probably know how to use advanced css selectors anyway and you can solve the sidebar dilemma in other ways, like :nth-child or adjacent siblings aside + section" see: http://www.yourhtmlsource.com/stylesheets/advancedselectors.html#adjacen...
TelFiRE’s picture

Code works on one Omega site and not on another :(

bmodesign’s picture

I just came over from using ZEN, so I started to freak out a little when this wasn't happening...

If you move around your sidebars to different zones, then try adjusting #16's code a little.

#16 in my template.php file worked great as long as I changed the code to reflect the zone my sidebar was actually in... and logged out.

*My alpha debugging block was still showing up in my sidebar because I allowed those to show up for my user-role. I logged out and this worked.

So take #16's code... and adjust part of it to reflect your zone (wherever you placed your sidebar).

<?php
function YOURTHEMENAME_alpha_preprocess_html(&$variables) {
  // Add information about the number of sidebars.
  if (!empty($variables['page']['content']['YOUR_ZONE_NAME']['sidebar_first']) && !empty($variables['page']['content']['YOUR_ZONE_NAME']['sidebar_second'])) {
    $variables['attributes_array']['class'][] = 'two-sidebars';
  }
  elseif (!empty($variables['page']['content']['YOUR_ZONE_NAME']['sidebar_first'])) {
    $variables['attributes_array']['class'][] = 'one-sidebar sidebar-first';
  }
  elseif (!empty($variables['page']['content']['YOUR_ZONE_NAME']['sidebar_second'])) {
    $variables['attributes_array']['class'][] = 'one-sidebar sidebar-second';
  }
  else {
    $variables['attributes_array']['class'][] = 'no-sidebars';
  }
}
?>

And refresh when you're logged out.

BUT if you moved your sidebar out of the CONTENT section and into the HEADER section for some reason, you'll need to adjust the code more.

So say you moved a custom made region, into another zone, that sits in the HEADER. (omega has header, content, and footer sections)

<?php
function YOURTHEMENAME_alpha_preprocess_html(&$variables) {
  // Add a toggle for the sweet section of content you want to design using CSS.
 if (!empty($variables['page']['header']['YOUR_ZONE']['YOUR_REGION'])) {
    $variables['attributes_array']['class'][] = 'YOUR-REGION-on';
  }
  else {
    $variables['attributes_array']['class'][] = 'YOUR-REGION-off';
  }

}
?>

You can use this alone, or just paste in the If-ELSE statement within #16's Cottser's function code to do both the sidebar statements, and more. Be sure to count your brackets.

And then add classes in your CSS for .YOUR-REGION-on and .YOUR-REGION-off

*For the noobs: To add a whole new region to your theme, check out the .info file in your sub-theme, and add it where it looks like it should go, right with the other regions. Same goes for adding a new zone. Then you can place the region and zones within OMEGA's theme settings page. The new ones will show up at the bottom of the zone list.

Hope this helps.
@BMDllc