Context Module in Drupal 7
Drupal’s Context module allows you to manage conditions and contextual reactions in the various parts of your site. This means that you can assign a different context for each “section” of your site and control the conditions that trigger each context, as well as associated reactions.
The Context module is a great way to create the separate “sections” of your site, each with different reactions based on “conditions”.
Web site with 3 mini sites
We were tasked with creating a Web site comprising 3 mini sites, each with its own blocks and menus, where any one block could be displayed on either one of the 3 mini sites, though in different areas.
For example, we had one single box to sign up for the newsletter, but this box needed to be displayed in different areas depending on the page you were on. The box also needed to be displayed in a different area on each of the mini sites’ welcome page.
Another requirement was for page visuals, for example menu colors, to change depending on context.
While Drupal’s basic block system does allow blocks to be configured on various pages or types of content, it requires them to always be in the same position.
Installing Required Modules
The Context module requires two modules:
In the following example, a different context must be defined for each mini site:
- Add a new context at “Administration/Structure/Context”.
- Name the context: for example, “Mini site1”.
- Under “Conditions”, you can use the Path, for example, “Mini site1”.
- In the “Reactions” field, select “Blocks”; the next window will show the list of regions and the list of blocks; you can then select the newsletter box, for example, for display in area x.
- To personalize the theme according to context: in “Reactions”, choose “HTML Theme” to add a CSS class to the page depending on context. Then use the CSS file to personalize each theme.
Modules to Extend Context
Context Rules allows you to create automatic triggers, conditions and reactions.
This is a simple but useful module that allows you to set a context to be active over a specified period of time, for example, from next Monday to Friday only.
Context Respect allows you to set overriding priorities whenever there is a conflict of visibility parameters between contexts and block configurations.
Another simple module, which allows you to redirect users to internal or external URLs when certain conditions are met.
Since contexts are linked to their theme and must know the areas of a theme to position blocks, in order to use a context in a new site, you must use the theme for which it was created.