Skip to main content

 

You are here

Why I'm fond of the Contrib Drupal module, Context

Mark Packham's picture
The Drupal Context module is by far my favourite module when working with Drupal 7 as it gives me so much control over the layout conveniently from within the Content Management System. The Context module works in a manner similar to the Rules module but dedicates itself entirely to the layout of your site. You provide it with one or more conditions and one or more reactions. Once the condition or conditions are met, the reaction(s) take effect. An example of a condition could be a user visiting a certain View Page, and a reaction could be the View Page themed in a specific manner that is different from the rest of the site.
 
 
The the majority of the time, I create a Path based on both a URL and node ID (in case the URL changes in the future) and use it to contain all the Boxes and Blocks I desire. For instance, I could create a condition that dictates that anyone who goes to a particular path on a site - perhaps a contact form or product page - they will see certain Block content, which would be the reaction. 
 
With the Path section of the Context module, you can use as many of these paths as you like with as much depth as you like. You can also have the Context apply things on every single page using an asterisk as a wildcard. You can also specify anything it should ignore using a tilde.
 
Context can be used to enforce a level of consistency in sites. For example, you may wish to devise a specific Node Type, e.g. a Lesson Webpage, to always contain identical aspects about each lesson. Using Context, you can ensure each node of that node type contain identical aspects such as a Menu Block in the top left corner to navigate to the various lessons and a Box in the footer containing identical terms and conditions for each lesson.
 
If you're working on a new site, Context is a good way of determining how a site has been laid out. Contexts are easily put into features generated from the Features module, and if you wish for subtle variations between sections of your sites then it is easy to clone Contexts.
 
With Context it's also possible to conceal Block content from various users. For instance, if you want to hide a sign-up Webform Block from users who have already registered to your mailing list, you would only want "anonymous" users to see it. To do this, you would look among your list of conditions, select the user role you would want to apply this to, and select whatever Block content you deem appropriate for only that user role to see. You could use the same process to have a restricted section of your site that only authenticated users could access. 
 
You will almost always have one Context dedicated to the front section of your website and one that is site-wide. The front page will usually have a View Block listing your latest Articles or maybe a slideshow, while the site-wide Context will often contain a Box in the footer with your company details. 
 
Something to keep in mind is which Contexts a specific Block resides in. An easy way to find out is to go to the Context module's settings at admin/structure/context/settings and tick Use Context Editor Dialog Refresh the page you've been looking at and click on the Block's Contextual link (the cog) and it'll show you which Contexts it belongs to.
 
Be sure to download Context. It has been around since Drupal 5, comes close to 1.5 million downloads and is still actively maintained. You can see a healthy amount of usage when it comes to Drupal 7 sites.
 
It requires Chaos tool suite (ctools) in order to work, given that most Drupal 7 sites have Views on them which also relies on ctools you'll probably have this module already.
 
You create your Contexts within the Structure section of Drupal. Be sure to enable Context UI or you won't be able to change anything. There is also a really useful introductory tutorial here.
 
A short example of the Context module's powers being enhanced by an additional module that removes Page Titles via the module Context No Title can be seen here.
 
There are numerous other plugins that can be added to give additional functionality to the Context module such as detecting if the person visiting a page is using a Mobile device, Context Mobile Detect.

0 Comments

Add new comment