Drupal Context Module Abridged
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.
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.
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.
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.
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 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.
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.
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.