Spiria logo.

Context Module in Drupal 7

August 8, 2016.
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”.

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:

Concrete Case

In the following example, a different context must be defined for each mini site:

  1. Add a new context at “Administration/Structure/Context”.
  2. Name the context: for example, “Mini site1”.
  3. Under “Conditions”, you can use the Path, for example, “Mini site1”.
  4. 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.
  5. 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.

Module Context

Modules to Extend Context

Context Rules

Context Rules allows you to create automatic triggers, conditions and reactions.
https://www.drupal.org/project/rules

Context Date

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.
https://www.drupal.org/project/context_date

Context Respect

Context Respect allows you to set overriding priorities whenever there is a conflict of visibility parameters between contexts and block configurations.
https://www.drupal.org/project/context_respect

Context Redirect

Another simple module, which allows you to redirect users to internal or external URLs when certain conditions are met.
https://www.drupal.org/project/context_redirect

Limitations

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.

See