What A Context Is

To really understand the Context module, a person has to first wrap their head around what a context is.

A context is the set of circumstances that form the setting for an event, statement, or idea, and in terms of which it can be fully understood and assessed.

Here's a simple, real-world example...

Context

We're planning a picnic in the park, in Corvallis, Oregon, during the month of May.

The event is the picnic. The context is the park, which is outdoors, during the month of May. Since it will be held in Corvallis, Oregon during this time, there are a couple of possible conditions that could occur...

Conditions

It could be sunny.

It could be rainy.

We have to determine the reaction we'll undertake, given a particular condition.

Reactions

If it's sunny, we'll have our picnic down by the river.

If it's rainy, we'll have our picnic up in the covered picnic area.

Your site has many different contexts and there are many different outcomes you, or our users, would like to see based on some of the conditions those contexts present.

Contexts In Your Drupal Site

The bulk of your work with contexts will probably be related to layout:

  • What does the front page look like?
  • What does the admin environment look like?
  • What does a user with a particular role see?
  • What blocks should display on the dashboard?

Here your conditions are:

  • Front page (path)
  • Admin environment (path)
  • User role (role)
  • Dashboard (path)

You can also combine conditions. For example, OSU Drupal 7 has default dashboard contexts that define the following:

At the path admin/dashboard, a person with an architect role will see the following blocks: My Content, My OSU Groups, Site Contacts, Group Management.

At the path admin/dashboard, a person with an author role will see the following blocks: My Content, My OSU Groups, Site Contacts.

The Context module is what we use to define and manage all of this.

But What About Block Settings?

If you've been working with Drupal for awhile, you're probably aware of the conditions that you can set right within a given block's configuration screen. And then, you can just, very easily, put that block into a region within the Block Manager at Admin menu > Structure > Blocks. We actually talk about all of this in the Blocks section of this technical manual.

This method works fine if you have a small, simple site. It quickly falls apart, however, if you have a large site with many sections, where you may want to use the same block in different layout configurations. Or, perhaps you're using Organic Groups and have a variety of different groups who may want to use the same block in different layout configurations.

"I can just make another block." You may say.

Sometimes you can do this, sometimes you can't. For example, if you have a block that is generated by a module you'll, most likely, only have one block to use.

If you try setting this up using the Block Manager page, you can only put it in one region. Maybe Group A wants the Video Carousel block in the sidebar but Group B wants the Video Carousel block down in the post content region. Who wins?

You sure don't. Neither do your groups.

Sometimes you can create multiple blocks, but often times you really don't want to do this.

Making multiple block displays with different filters will very quickly become a management nightmare for you both in terms of managing your layout and in terms of managing the settings within your view. Imagine that you have a site with 50 different groups in it and they all want their own special news feed block. That would be FIFTY news feed blocks alone that you would have to manage.

Instead...it's much easier to just build a view with a contextual filter that filters Announcement nodes based on a group id and then define two contexts where "Announcements block shows in the sidebar" and "Announcements block shows in the post content region". Then, you just add the respective groups to the context they want. This way, you have an extensible method. It doesn't matter if you have two or two-hundred groups, there will still only be one block and two contexts necessary to give your people the layout option they want.

Let's take a look at how to do that...