How to use Drupal’s Context Module


“If …, then …” is one of the most commonly used phrases in the world of software development and, I suppose, in life:

  • If the sky is blue, then it probably won’t rain.
  • If I learn Drupal, then I’ll make lots of money.
  • If the webpage URL includes “services” in the path, then display the block called Services.

In this blog, we are going to introduce to you to the Context module, which puts the power of “If …, then …” into your hands.

Sample scenario

Here’s the example we’re going to use to introduce Context:

Imagine you want this block to show in the first sidebar region on the home page but in the footer area on all other pages.

However, your block is generated by a module. A good example is  the “Who’s online” block. Our problem is that it’s not easy to duplicate a block that is created by a module.

Here’s how we’d use Context to solve this problem …

Install the required modules

There are two modules required for Context to work.


Once the modules are on your server, you will be able to see them in your module list.

  • Click on Modules in the black admin menu bar.
  • Check the box next to Context and Context UI modules.
  • Click Save configuration.
  • Drupal is smart enough to know which modules are required and will now prompt you with a list of modules it will enable for you. They include the Ctools module.
  • Click continue to finish enabling the modules.

Create the homepage context


In our scenario, we want the “Who’s online” block to appear in a sidebar on the home page but in the footer on all other pages. To accomplish this, we will create a context for the block on the home page.

  • Click on Structure and then on Context
  • Click on Add

Name your context

  • Name your context “homepage”

Configure the “if”

  • In the Conditions dropdown, select Path
  • Observe that “Path” now appears below the dropdown
  • Type <front> in the text box.

Note: this path condition is the same as the default Pages visibility setting on the block configuration.

Configure the “then”

  • In the Reactions dropdown, select Blocks and observe that “Blocks” appears below the dropdown.
  • Scroll down and check the box for the Who’s online block
  • Click +Add next the Sidebar first region

Check the settings

  • Observe that the block in no longer in the list and is showing in the Sidebar first region.
  • Scroll down and save.

Observe the placement of the block


Notice that the block appears first, above the Search and Navigation blocks. This is important. In this scenarion, if you want the Search block to appear above the “Who’s online” block, you will need to add the Search block to the homepage context and position it above the “Who’s online” block … just as you would do in the default block configuration interface. Then you will want to remove the Search block from the default block configuration.

Configure the block for the rest of the site


We could create another context that displays the “Who’s online” block in the footer for all other pages or we can use the default block system. For demonstration purposes, let’s use the default block system.

  • Click on Structure and then on Blocks
  • Click configure next to the “Who’s online” block
  • Select the Footer first column region
  • Select “All pages except those listed”
  • Type <front>
  • Save

Observe the block in the Footer first column

  • Notice when you click on a page on your site, the block does not show in the first sidebar but now shows in the footer.

More on Context

We have another Context tutorial showing how to place a block on different content types.

As you can imagine, using the Context module will require some exploration on your part. Don’t be afraid to play with the conditions and reactions to see what you can do and if the options are what you need.

Have fun with this module. It is very powerful and once you get the hang of it.


0 0 votes
Article Rating
Notify of

Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

this article helps me. thanks a lot and sorry for my bad english! =)

9 years ago

Really informative and crisp post. Keep it up!

Srikanth Ganta
Srikanth Ganta
8 years ago

Context is nice module but the issue is,can i use it as a complete alternative to other layout related modules like FPP/Panels/pane/Panelizer,Display Suite,Boxes,Bean?

Would love your thoughts, please comment.x