When you need to dynamically display a block within a region, nothing can really beat the Context module. It allows you to define a set of conditions, that when met, executes a set of reactions. For example, you could create a context rule which adds a block to the sidebar second region (reaction) when a user is viewing an Article content type (condition).
A lot of what Context does can also be achieved using Panels. But if you’re new to Drupal, and never used Panels than Context will be easier to use.
If you’re new to Context then I would highly recommend you watch the two introductory videos below:
- Drupal 7 Context module – Daily Dose of Drupal episode 72 by Shane Thomas
- Drupal Tutorials #63 – Intro to Context – Adding Blocks With Context by LevelUpTuts
Recently I’ve discovered a powerful module called Context entity field. It allows you to define a condition that checks the value of a field on an entity.
Let’s say you have a use-case where a block should only appear if an editor selects a tag like “Article”. Using Context entity field you can create a condition that checks the Tags field if a specific term has been selected.
In this tutorial, we’ll use Context entity field to display a debug message only if a specific term is selected from the Tags field.
For this tutorial I’ll use the
If you use Drush, run the following two commands:
drush dl context context_entity_field-7.x-1.x-dev ctools
drush en context_ui context_entity_field
Create Context Rule
Let’s first create a context rule that will be used to display a debug message.
1. Go to Structure, Context and click on Add.
2. Enter in
article_block into the Name field.
3. In the Conditions section, click on the
<Add a condition> drop-down box and select Entity field.
4. From within the Entity field condition form select Node from Entity type, field_tags from Field name and Match from Field status.
5. Then in Field value enter in the term ID which you want to match on.
Now let’s take a quick detour and talk about the “Field status” option. A field’s status can be evaluated in three ways: “All value”, “Empty value” or Match.
The “All value” option is useful for when you want to check if a field has been filled out. Then the “Empty value” is useful for when you want to check if a field is empty. And finally, use the Match option for when you want to match on a specific value.
This means that if a term with an ID of 1 is selected from the Tags field then the context rule will execute. Also understand, that the Match option may not work with every field out there so make sure you test it out. If you’re having trouble look at the
6. From the Reactions section, select Debug from the
<Add a reaction> and then click on Save.
The debug reaction will simply display a message saying that the reaction has been executed.
Test Context Rule
Now that we’ve created a context rule using Entity field condition let’s test it out.
1. Go to Content, “Add content” and Article.
2. Enter “Test article” in the title field and select a term in the Tags field that has an ID of 1.
In my Tags vocabulary I have two terms: Article (term ID 1) and News (term ID 2).
3. If I select Article from the Tags field, the context rule will execute and I should see the “Active context: article_block” message.
The example I’ve shown in this tutorial is pretty basic but you get the idea behind the module. Now, before we finish up, the module comes with a second context condition called “Entity view mode”.
This condition is useful for when you want to run a reaction on a specific view mode like teaser.