Drupal: Context and why its better than just using blocks

One of the best things about Drupal is that there are many different ways to do the same thing, I would probably say that this is one of the curses of Drupal as well.  I would guess that 95%+ of the Drupal powered websites use Drupal blocks for some elements of the site.  Blocks are a great part of Drupal, if you are more familar with WordPress a Drupal Block is very similar to a WordPress sidebar Widget.

Blocks are the boxes visible in the sidebar(s) of your Drupal website. Most of the blocks that you will see (e.g., recent forum topics) are generated on-the-fly by various Drupal modules, but you can also create your own blocks.

Blocks do not have to be placed in the sidebar of a page, a block can be added to various regions on a page, header, footer, top of content, bottom of content, first sidebar, second sidebar, or any other area defined as a region within your theme.  The default method of adding blocks to a page is by using theme specific settings on the Blocks admin page.  The default Garland theme Block configuration page could look something like this:

The first issue I’d like to point out is that when you’re using the default method of adding blocks to a Drupal site, if you change themes you have to set up the blocks again for that second theme.  Another issue is that if you place a block in the “Right Sidebar” it will always be in that region.  Meaning that if you had a block that you wanted to show up on right side of the page for pages X,Y and Z, and you wanted the same block to show up on the left side of the page for pages A,B and C you would have to duplicate the block.  These were a couple of the first reasons that I started to use the Context Module.  (I have since discovered many more reasons to use and love Context, which I can share at a later time.)

Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a “section” of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Think of conditions as a set of rules that are checked during page load to see what context is active. Any reactions that are associated with active contexts are then fired.

After you install Context and the Context UI, you will have a UI, similar to Views, for adding a Context.  Below is a basic example of a sitewide context to add blocks to different regions of the site.

This is the top portion of the Context form, showing the Name, Tag and Description of the context.

The next portion of the context editor lets you set the conditions, or ‘context’, and then the reactions for the context.  In this case, I have chosen a ‘site-wide’ context, which means exactly what it says, the entire site is the context and for the reaction(s) I have chosen Blocks.  This allows me to select specific blocks to show up in specific regions for the entire site.  In this case the footer is a block and I want it to show up in the footer region for the entire site and the primary navigation and search form are blocks and I want them to show up in the header region for the entire site.

By using this Context, if I change themes, as long as I still have those regions, my blocks will continue to show up in those regions for the entire site.  I do not have to worry about setting the Blocks to display on the Blocks admin page.  I can also define different contexts for different sections of the site, allowing for the same block to be shown in different place on the site (if needed).  But note, that you can not use the same block more than once within the same context.

I highly recommend Context for every new site, there are some minor issues that have to be addressed if you are using the Zen theme (and even some others) but nothing terrible (make sure you read through this thread if you are using Zen 2.x specifically comments #6 and #19).  Context is now on my list of modules that I ‘can’t live without.’

I will continue to post about modules, themes and site building strategies, I will be tagging everything in the Development Tips category so please feel free to check back in and leave some comments!