What the Heck is Drupal Context?

Janet Manning // May 2018

Well, let me put it in context :-) The Context module is one of the most popular in Drupal 7. It is a great tool to place custom blocks on a page in any number of locations within a website. To use the module, there are usually two contexts created - one with an entity for placement and another for content, allowing for the placement of blocks on content entities. The Context module allows you to set up different profiles for different sections of a site.

Context conditions exist as if, then “statements” where

  • The Condition is the If — if one or more of the conditions are met
  • The Reaction is the Then — the behavior triggered by the presence of the context rule parameters

An example of why you would want to use contexts

    Say you have an alert block showing upcoming downtime that needs to be placed:

    • On the homepage for all page visitors to see - placed within the utility menu at the very top 
    • On the client login page - because you know that a high percentage of clients enter the site there, you need the alert to be very prominent, placed at the top of the content area on the page
    • On a few landing pages within the site - it does not need to be as prominent on these pages so you want the placement to be unobtrusive.

    Creating context conditions to handle the three scenarios above is a good idea because: 

    • It eliminates the need to use the core Blocks module for placement; performance may be improved by using Context
    • Context is easy to use with an intuitive drag-and-drop UI so it is great for site builders (it is much less complex than Display Suite and Panels).
    • If you change the theme, you just need to make sure to include the same regions for block placement

    To use Context

      You need to install both the Context module and Chaos Tools module (must be version 1.7 or higher). 

      Included with the Context module are:

      • The Context UI module that provides an administrative interface to manage contexts
      • The Context Layout module that allows themes to declare and switch between templates using contexts

      Required to create a contextual condition are:

      • Name
      • Condition
      • Reaction

      Context for Drupal 8

      While the block system in Drupal 8 is amazing, you may still want to consider using the Context module to take advantage of the condition-reaction flexibility.

      The Drupal 8 version of Context was ported from Drupal 7 and uses the built-in condition plugins supplied by the Plugin API. Other conditional plugins can also be used with Context.

      Janet Manning Headshot

      Janet Manning

      Engagement Manager/Training Coordinator

      I partner with internal and client creative and technical teams to build  beautiful, flexible and scalable sites with an amazing user and admin experience. Once a project is underway, I work closely with the design and development team to keep projects on time, on budget and within scope.