Drupal Context Module Abridged

This post is part of our Abridged series, which aims to explain the basics of some of the more ominous yet awesome Drupal projects in simple and practical terms. We hope these posts will help demystify some of these projects for people who have been hesitant to try them out!

In this post we'll take a look at the Context module.

Background

The Context module is developed by the Washington DC Drupal shop Development Seed. It is one solution to the Drupal problem of using multiple pieces of code scattered throughout a site asking questions such as "Is an event node being displayed?", "Is a view of events being displayed?", or "Is an event form being displayed?" Context enables a single piece of functionality that asks "Is the events section being displayed?" This can be incredibly useful to control active menu trails, block visibility, and theme layouts and classes.

A context becomes active if one or more conditions are met (see below for a full list of conditions). If a context becomes active it will cause one or more reactions. A simple context example is to react by displaying the recent comments block in the sidebar region if the condition of viewing a story node type is met.

How To Use It

As of this writing, the latest version of Context is version 6.x-3.0-beta5. The latest recommended release is 6.x-2.0. This post will be referencing the 3.0 version.

The Context module includes the modules context_ui (the administrative user interface) and context_layouts (more on this below). It's recommended to also use the jQuery UI and Admin modules to take advantage of the Context inline editor.

The Context user interface is one of the more straightforward that I've used. Administer > Site building > Context will list current Contexts.

The only requirements for a new context are a Name, Condition, and Reaction. You may add a Tag to a context which will order contexts on the context listing page. A checkbox 'require all conditions' can be used if you want all selected conditions to be true for the context to be active. Leave this unchecked if you only want one of the selected conditions to be true for the context to be active.

In the image below the context 'site' is always active as it is a sitewide context and the reaction is placing the "Powered by Drupal" block in the "Content" region.

Once a context has been created, it may be exported into a feature and stored in code. To learn more about the Features module take a look at our Features Module Abridged post. 

Conditions

Book (dependent on book.module) - Set context when a node in the selected book is viewed.

Book Root (dependent on book.module) - Set context when viewing a node whose root book is of the selected type.

Language (dependent on locale.module) - Set context when viewing the site in the selected language.

Menu - Set context when one or more menu items belong to the current active menu trail.

Node Type - Set context when viewing a node page or using the add/edit form of one or more content types. Specify whether or not to set context on add/edit form.

Taxonomy (dependent on taxonomy.module) - Set context when viewing a node with one or more selected taxonomy terms. (Does not work with freetagging vocabularies.)

Path (dependent on path.module) - Set context when any of one or more paths match the page path. The "*" character can be used as a wildcard and <front> can be used for the front page.

Sitewide Context - Set context across entire site.

User Role - Set context when the current user has one of the selected role(s).

Views (dependent on views.module) - Set context when displaying the page (not block) of one of these views.

Reactions

Blocks - Display a block in a region.

CSS Injector (dependent on css_injector module) - Inject a css rule defined by CSS Injector.

Debug - Simple reaction using drupal_set_message() to test whether context is active.

Menu - Add the 'active' class to a menu item.

Theme - In page.tpl.php set $section_title, $section_subtitle or add a class to $body_classes.

Context Editor

The Context editor in the Admin menu is a handy tool for testing and editing contexts. You can view which contexts are active on any page of a site as well as edit them by adding conditions, blocks (drag and drop) and theme variables.

Context Layouts

Context Layouts provide a formalized way for themes to declare and switch between page templates. By adding information into your theme's .info file you can make multiple layouts available. More information can be found in the module's README.txt.

Creating an Example Context

For this example we will create two contexts. One will be a site-wide context that displays the "Powered by Drupal" block on every page. The second, a story section context, will display a recent comments block when either a story node or the stories view page is being displayed. The primary links menu item "Stories" will also become active when this context is triggered.

To begin be sure that all your sites block's are disabled at Adminster > Site building >Blocks.

To create a new context go to Administer > Site building > Context and click "Add". We'll name our sitewide context "site". We don't need to click "Require all conditions" as there is only one condition. Add the condition "Sitewide context" and click "Always active". Add the reaction "blocks" and check the 'Powered by Drupal' block and click "+Add" in the region you want the block to be displayed. Click "Save". You've created your first context! Wherever you go on your site, the 'Powered by Drupal block' will appear.

Now we'll create our stories section context. First, create a simple view that lists story nodes with both a Blocks and Page display. For the page display add a primary links menu item. Add a new context and name it "stories". We don't want to check 'Require all conditions' because we'll be using two conditions and want the context to be triggered if either condition is met, not both together. The first condition we'll add is "Node Type" with the "Story"node type. Uncheck "Set on node form" as we don't want our "Recent comments" block appearing on node forms. Add a second condition of "Views" and choose the story listing view. Add a "Blocks" reaction selecting the "Recent comments" block into a region. Save the context.

Install Profile of Example Context

The contents of this example can be quickly built using my Simple installation profile and Simple Context feature.

I've created a build file, simplecontext.build, that uses both the install profile and feature and which can be used to create the example Drupal codebase using drush make. You'll need to have the latest versions of drush and drush make to run the following command. For more information on drush make please see this blog post by Greg Harvey, How Drush Make Just Changed My Life.

drush make --contrib-destination=profiles/simple simplecontext.build simplecontext

This will create a folder named simplecontext and build a Drupal codebase within it including the Simple Installation Profile and the Simple_context feature. The "--contrib-destination" flag will make sure that the simple_context feature is saved within the profiles/simple directory along with other contrib modules and themes. Install this site like any regular Drupal site and during the installation process choose the "Simple" installation profile.

After installation go to Administer > Site building > Features and enable the Simple Context feature.

Resources

The Context module project page includes links to the README.txt, API.txt as well as links to number of posts and presentations.

The Heart of Open Atrium: Context, PURL and Spaces - DrupalCon San Francisco 2010 

How We've Used Context at Affinity Bridge

We've recently been using Context in place of Panels for a number of projects. Where we used to define a number of panel panes on a panel page, we have created a context which reacts to set block visibility. We've also done a small integration with the Design Kit module using Context to set a class and define custom header images for sections of a website.

 

Comments

Great write up

Great write up Shawn. As always it's concise and easy to understand. Thanks for the link to the simple profile for context also.

context theme question

Theme - In page.tpl.php set $section_title, $section_subtitle or add a class to $body_classes.

how do I use/call these vars in the page.tpl.php
is it necessary to define all three?
is there documentation?

I'm attempting to change an id in a div based on the path in the page.tpl.php

Thanks

Sorry I missed this comment

Sorry I missed this comment when it was posted.

The two additional variables, $section_title and $section_subtitle are used by conditionally checking for them in page.tpl.php. If activated Context will provide them.

When using the Context theme reactions you can use any number of the three available options.

  • Using "Section title" will provide the supplied text as the variable $section_title in page.tpl.php when the context is activated.
  • Using "Section subtitle" will provide the supplied text as the variable $section_subtitle in page.tpl.php when the context is activated./li>
  • Using "Section class" will add the supplied text to the $body_classes variable in page.tpl.php when the context is activated.

I've found that Context is mostly self documented in the code so it takes a little bit of poking around. Here's a link to the documentation for context.module on api.lullabot.com.

From the sounds of what you are trying to do with changing an id in a div I don't think Context is the answer. You could supply one of the Section variables as a div class if you really wanted to.

thanks for the reply -even

thanks for the reply -even though it was late

I figured it out through a slightly different method.
I used css injector to add/remove a content id based on the path and
context to change the layout (1/2/3-left/right columns) also based on the path

I was trying to get rid of a content background on certain image based pages
(the images took the place of the background)

excellent approach

about time someone started explaining drupal modules in english. mostly, drupal community just explains things in terms of code. for me, i always need to know what it does first. what it does, why it does it & how it does it (but not necessarily in terms of code & functions)

please do lots more of these (including 'spaces' & 'ctools'). i will read them all

PS: actually, i'd like it if u could explain larger patterns in common sense terms as well. i mean, say you want a site that lets people collaborate on projects? do you use blog, blog (built without 'blog'), forum, OG, or a combination? what would be the pro's & con's of each? [ stuff like that ]

Thanks for the kind words.

Thanks for the kind words. I'll keep your suggestions in mind for the next post.

And I've personally had more luck with a custom content type than with blog.module ;)

facebook pages theme with context

Thank you for your Drupal help Shawn, I appreciate it.

I have been using context on a few sites and now on my own which has become complex. Now, using /project/fb, I need to switch themes for facebook pages at 760px. This looks to me like I will have to do a lot of work by duplicating all or most of my "context" to move blocks around and eliminate a sidebar, plus set a "reaction" flag to CSS to override my "fusion" grid widths and likely a few more tweaks.

Would that be the best approach?

Thanks, Jerry

Thanks! I haven't used Drupal

Thanks!

I haven't used Drupal for Facebook so can't comment on a best approach to your problem. I'd suggest filling a support request in the issue queue.

Good luck!

Great module intro! thank you

Great module intro! thank you a lot!

Hi, I am using context 3.0 in

Hi,

I am using context 3.0 in Drupal 6.19 & 6.20 both versions. But I am not able to see blocks defined in reactions section of context.

Is there any patch required??

I haven't been able to get

I haven't been able to get Blocks reactions to NOT show up in Context. I tested it out using the default profile of Drupal 6.20, views 2.12, ctools 1.7, and context 3.0. You may have better luck by searching and posting to the Context issue queue. Good luck!

Is there any way to set the

Is there any way to set the context programmatically? What is the best way to toggle a reaction based on whether or not a user is logged in for a example?

To set a context from code

To set a context from code use context_load() and context_set().

Given a context named 'user_logged_in', you can choose the appropriate hook and conditional logic to place the following code into.

context_set('context', 'user_logged_in', context_load('user_logged_in'));

Thank you for the post!!!

Thank you for the post!!!

tip

It is also nice to use context_active_contexts() to get an array of active contexts in a given page.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options