3

In some cases I need to programmatically remove the sidebar_first region in template.php. So, I'm doing something like:

function template_preprocess_page(&$variables) { if(my_condition){ unset($variables['page']['sidebar_first']); } } 

this does, indeed, remove the sidebar, however, it does NOT alter the body classes. So, I still have a class of

body.two-sidebars 

...even though I unset one of them (so the proper class would be body.one-sidebar).

How can I programmatically remove a sidebar AND have it alter the body classes properly?

(please, for various reasons dont suggest creating a new template suggestion and altering the sidebar there. that won't work in my case. I am asking specifically how to remove a region programmatically)

3 Answers 3

3

If you look at the default template_preprocess_html, you will see

 // Add information about the number of sidebars. if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) { $variables['classes_array'][] = 'two-sidebars'; } elseif (!empty($variables['page']['sidebar_first'])) { $variables['classes_array'][] = 'one-sidebar sidebar-first'; } elseif (!empty($variables['page']['sidebar_second'])) { $variables['classes_array'][] = 'one-sidebar sidebar-second'; } else { $variables['classes_array'][] = 'no-sidebars'; } 

If you don't want this behavior, you should implement your own version which will run after. Then, you just check your condition and muck with $variables['classes_array'] as needed.

4
  • But, I can't detect if I should unset it here because my condition that I'm checking is in $node...and $node isn't an available variable in preprocess_html Commented Sep 14, 2012 at 19:17
  • 1
    If template_process_page is no good, then you can certainly remove the class in template_process_html. You should be able to find some hook that runs before template_process_html (e.g. template_preprocess_page) that has access to $node; you could set a flag at in that function, and act on it in template_process_html. Commented Sep 14, 2012 at 19:33
  • if I could get that "flag" in preprocess_html that's probably what I need. How would I set that in page and pass it to html? Commented Sep 14, 2012 at 19:48
  • 1
    Isn't $variables['page']['sidebar_first'] unset in preprocess_html if you've already altered it in preprocess_page? Commented Sep 14, 2012 at 20:28
2

You should be able to find the information you need in $variabless['classes_array'], which contains an array of body classes. Just remove the one whose value is two-sidebars.

3
  • Yup, thats what I thought too. But the classes array is empty. Commented Sep 14, 2012 at 19:13
  • Try removing it in template_process_page instead of template_preprocess_page. Commented Sep 14, 2012 at 19:24
  • in both preprocess and process my classes_array only contains one item, which is 0 => page Commented Sep 14, 2012 at 20:00
1

I basically figured out that the problem is that you have to do this in two different template.php functions: template_preprocess_page (to kill the sidebar) AND template_preprocess_html (to get rid of the body class I didn't want).

so first in preprocess_page:

$variables['page']['sidebar_first'] = NULL; 

then in preprocess_html I had to get node info with menu_get_object(), check the node data for my condition, then parse the $variables['classes_array'] to manually replace the 'two-sidebars' class and add anything else I want.

Sheesh. Should be much easier, but I thank the guys in the other answers for helping me confirm it's not as simple as it should be.

2
  • I'm glad you figured out your problem. Most themeing tasks do usually boil down to messing around with the $variables array in a preprocess function. Drupal 7 split out html from page, which helps in some places, but does complicate others. That said, my advice or @greg_1_anderson's should have worked. Something doesn't seem quite right with the symptoms you described. Commented Sep 14, 2012 at 22:09
  • Just noted: A side-effect of removing the sidebar(s) from the page as you describe is for the correct no-sidebars or has-one-sidebar whatever classes to be set on the <body>. So if you have no other requirements for the body classes, you need not bother parsing / updating $variables['classes_array']. Commented May 9, 2015 at 9:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.