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.
Getting Started
Head over to drupal.org and download Context, Context entity field and Ctools. Then install Context UI and Context entity field.
For this tutorial I’ll use the
7.x-1.x-dev
branch.
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.
Field Status
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 get_field_value()
method.
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.
Summary
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.
Hi Ivan,
I’m the author of Context Entity Field. Thanks for the guide (can I link it from module docs?).
I’m working on two large improvement:
If you have some suggestion please write me 🙂
PS: why are you using dev version?
Thanks for the module, and yes you can link to it.
The reason why I used the dev version was because I got a notice warning when I was testing the module.
“But if you’re new to Drupal, and never used Panels than Context will be easier to use.” I dont know if I agree on that. I think Panels is easy and straightforward. I think I will do a post to do the same in panels, just to compare.
Fair enough. I’ve worked with web developers who are new to Drupal and the Panels interface can be overwhelming at first. I’m not trying to belittle Panels, it’s a powerful module. Once you know how to use it a whole new world opens up. 🙂
Let me know once the post is published, I’ll link to it.
This module is just what I needed!
Is it possible to add multiple values that satisfy the condition? ex for term id: 1,5,9
Thanks
Hi George,
I haven’t tried this myself. But I did find this issue about multi-value support. https://www.drupal.org/node/2045809
Cheers,
Ivan