1

I use Drupal 7.43 with Context to hide the sidebar regions in certain content-types so that their nodes will have full-width (at least in desktop mode).

This is a good way to hide sidebars programmaitcaly, and not by CSS (which may cause clashes with other CSS rules and maybe also JavaScript).

My problem:

If I change my theme, I need to reapply each condition-reaction I've created to regions in the new theme. For example, I need to pick the existing name of my sidebar region each time anew.

Do you think there's a better approach to auto-hide ANY sidebar region on certain Pages or Nodes?

2
  • If there's nothing inside the sidebar regions, they should not afect the width of the content. You could also create a page template for the content types you want and remove those sidebar regions. Commented Mar 21, 2016 at 14:20
  • There usually is.
    – user16289
    Commented Sep 6, 2018 at 18:04

1 Answer 1

1
+50

You could do this through a very simple custom module. This way hiding the region will work with any theme, as long as it has the same name of the region you are trying to hide.

Here's the code that would go into your sample.info file:

name = Module name
description = Module description
package = Custom
core = 7.x
version = 7.x-1.0

Create sample.module file and add the following code:

function sample_page_alter(&$page) {
  // Fetch the node object.
  $node = menu_get_object();
  // Perform some checks to make sure this is the page we want to alter.
  if (
    // We are on node page.
    $node &&
    // Check if the node if of type you want to have 100% wide layout.
    $node->type == 'page' &&
    // This will exclude local node tasks, such as edit, delete and revision.
    !arg(2)
  ) {
    // Unset the region you want to hide.
    unset($page['sidebar_first']);
  }
}

Update

You asked for a wildcard that would work across multiple themes.

First off - the sidebar_first and sidebar_second are somewhat standard names for left/right sidebars in Drupal themes. Most of the themes will name sidebars the same way.

However, for the sake of answering the question, here's how you'd "scan" for all regions that contain for example the word "sidebar":

function sample_page_alter(&$page) {
  // Fetch the node object.
  $node = menu_get_object();
  // Perform some checks to make sure this is the page we want to alter.
  if (
    // We are on node page.
    $node &&
    // Check if the node if of type you want to have 100% wide layout.
    $node->type == 'page' &&
    // This will exclude local node tasks, such as edit, delete and revision.
    !arg(2)
  ) {
    // Unset the sidebar regions.
    //
    // We will loop through the elements and check if they are set and contain
    // the word "sidebar".
    foreach (element_children($page) as $region_name) {
      if (strpos($region_name, 'sidebar') !== FALSE && !empty($page[$region_name])) {
        unset($page[$region_name]);
      }
    }
  }
}

Notes

  1. Do take into consideration that the above approach will just prevent the region from being printed on the screen. This means that if you have complex blocks, they will still be rendered and take server resources.

  2. You can replace all instances of sample with whatever else you want to call your module.

For more information, check out the following:

Hope this helps.

4
  • One small question Aram, maybe this could be done with a wildcard, instead of counting on a name such as "sidebar" for any theme, I guess we can do this pseudocode: unset($page['sidebar']) || side-bar || side_bar || and that all cases with either sidebar, side-bar or side_bar will be effected... Sorry if this pseudocode looks funny, I am very new to PHP, just learning some basic stuff... I am still not there :|
    – user16289
    Commented Mar 26, 2016 at 17:02
  • 1
    Updated, though it doesn't make much sense to perform a more complex pattern matching there to remove the regions that will most likely be called sidebar_first and sidebar_second. Let me know if that's what you were asking. Commented Mar 27, 2016 at 17:19
  • Indeed it was, thank you very much for your answer and edit, I've a learned a total lot from it!
    – user16289
    Commented Mar 27, 2016 at 18:45
  • 1
    You're welcome - glad it helped :) Commented Mar 27, 2016 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.