Submitted by admin on Fri, 09/06/2019 - 16:08

"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.

Image removed.

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

Image removed.

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

Image removed.
  • Name your context "homepage"

Configure the "if"

Image removed.
  • 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"

Image removed.
  • 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

Image removed.
  • 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

Image removed.

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

Image removed.

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

Image removed.
  • 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.
Solutions Img