The Drupal project page for Context describes it in this way: “Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a “section” of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.”
Here’s what that means to you: “you can make blocks, themes and more show only where you want them.”
Context is a very powerful module, and many sites use it instead of Drupal’s normal block system. In this tutorial we’re going to show you how Context works by making a block visible on only one content type.
Step 1. Install and enable Context
Here’s what you need in order to get Context enabled on your site:
- Context: http://drupal.org/project/context. Included in the module download are Context, Context UI and Context layouts.
- You will also need Chaos Tools Suite from http://drupal.org/project/ctools.
- If you’re using Drupal 6, we recommend also installing jQuery UI from http://drupal.org/project/jquery_ui and Admin http://drupal.org/project/admin.
The Drupal 7 version of Context is still in beta but is stable enough for use on most sites. The Drupal 6 version, works the same, but the interface looks slightly different.
Step 2. Get started
Go to Structure > Context and click Add.
Step 3. Create a new context
Start with a name, tag and a description. The rest of the interface will be visible after you fill in these fields. and make selections from the drop down menus on the lower part of the page.
- Name: The name of your context. This is the main identifier for your context and cannot be changed after you’ve created it.
- Tag: A category for organizing contexts in the administrative context listing. Optional. Examples of organizational tags would be block or theme.
- Description: A description or human-readable name for your context. This is displayed in the inline editor if available instead of the name.
- Require all Conditions: If checked, all conditions must be met for this context to be active. Otherwise, the first condition that is met will activate this context.
- Conditions: Trigger the activation of this context.
- Reactions: The action to take when the conditions are met.
The empty red square shows where your conditions will be listed. Once you choose a condition you’ll see what I mean.
Step 4. Set the first condition
- Click the dropdown arrow to view the list of conditions.
- Click the one you want to make a condition for this context.
- Choose Node type.
What you are actually doing is creating a set of “if-then” statements, but using the interface instead of writing code. Conditions are the “if” statements. If you know any basic programming you are familiar with conditional statements. Parents use these a lot: “If you eat your peas, then you can have desert.” What we are going to do is create a group of these if-then statements. The entire group of statements is a “context” in Drupal terminology.
Step 5. Choose the details of the condition
Check the box next to the content or node types you want to have the blocks appear.
I am going to add our block to a content type we created in a previous tutorial called Testimonial. When someone reads a testimonial we want the to see who’s online and who just joined us so we’re going to put those blocks on every testimonial page.
You can also see that Node Type now appears under Conditions. All the conditions we create will be listed here, and linked to their controls so we can come back and edit them if we need to.
Step 6. Choose a reaction
A reaction is the second half of the “if-then” statement.
- Make your selection from the right column. I chose Who’s new.
- Click “Add” in the region you want to place the block.
- Scroll down and Save.
- The block will now be placed in the right sidebar. Similar to using Block, but with one big exception, the Who’s online block will only show when all conditions are met. So if you are an administrator, and if you are viewing Basic page 1, 2, or 3; then you will be able to see the who’s new block.
- Choose Blocks.
- Click the check boxes next to the blocks you want to show.
- Click Add on the region you want the blocks.
- They will now be listed under the region. The weights will determine the order they appear on the site.
- Save by scrolling to the bottom of the page and click save.
- Now you’ll see a confirmation message:
Step 7: Check the node type from the front end
If all is well you will see the Who’s online block and the Who’s new block only on the nodes you chose as conditions as in the image above. Those blocks won’t appear on other nodes, as in the image below.
If you want to get more refined, you can go back and add another condition. For practice, try adding user role as a condition so that only logged in users can see the block. You can add more conditions and more reactions until you get a fairly “fine-grained” filter built. You might also try applying a different theme to a content type. It’s even possible to have one context trigger a second context.