Context has become an essential module for site building. Ever since its release in Drupal 6, I have used it on many Drupal websites. Due to its popularity and flexibility, there are a lot of contributed modules that extend Context even further.
I won’t go into details about what Context is, but if you want to learn more check out the answers in this Drupal Answers question.
The three modules that I’ll discuss today are: Context Node, Context by Node and Context Field. The module names may be a bit confusing but they all serve a particular purpose.
In this article, we’ll have a quick look at all three modules and see how they work and differ.
UPDATE: Thanks to a comment, there is a fourth module called Context entity field. Be sure to check it out.
The Context Node module allows users to select a predefined context from the node add/edit page. The chosen context will then be executed on the node that selected it.
For example, you could use Context to control the look and feel of an article page. When an editor creates an article, all they have to do is select which context should be used. This allows you to reuse the same context rule across multiple node pages.
How to Use Context Node
Configuring the module is fairly simple. Once you download and install it, go to Structure, “Content types” and click on Edit within the Article row.
Scroll down and click on the “Allowed node contexts” vertical tab. In this tab, you can select which contexts should be available to the user and you can define a default one.
Now if you go to the “Create article” page, you should see the Context vertical tab.
Select which context you want to use for the specific article and you’re done.
Context by Node
Context by Node on the other hand works differently. The module generates a context rule specifically for a single node. Let’s say for example you create 1000 nodes, then you’ll end up with 1000 context rules, one for each node. You get a lot of flexibility, but there are a lot of context rules to manage.
This module is useful for when you want to customize each and every article using Context. Editors can use Context’s UI to configure custom conditions.
How to Use Context by Node
Download and install Context by Node and make sure you enable the Context UI sub-module. Then go to Structure, Context and click on Settings.
From this page you can configure which content types can generate a context, and you can also hide the generated ones from displaying in the Context administration section.
Now if you go to the “Create Basic page” page, click on the “Context by Node” vertical tab and check “Create Context for this Node”. When you save the form, a context rule will automatically generate.
After you’ve created a basic page head over to the Context page in the administration section, and you’ll see a generated context rule.
You can also directly access the context edit page by editing the node and clicking on “Edit context”.
The Context Field module offers the same functionality as the above mentioned modules but it implements its functionality using a custom field.
The implementation of a field is the biggest benefit of using this module, you can use it on nodes as well as on taxonomy terms, users and any fieldable entity.
This module is very powerful and deserves its own tutorial on Web Wash. For now, I’ll quickly explain how to get started, but expect a full tutorial some time in the future.
How to Use Context Field
To use the module, simply download and install it. As of this writing, I recommend that you avoid using version 7.x-1.0-beta4 because it has a dependency on the Boxes module. The latest development release removes this unnecessary dependency so for now use the 7.x-1.x-dev version.
Once installed, go to Structure, “Content types” and create a context field on an article content type.
Now if we create an article, the field should have automatically created a context. If you go to Structure and Context you should see a context rule called
So which module should you use? If you want to control context rules on content types, then try all three. If you want to control context rules on any other entities like terms or users, then use Context Field only.
If you have any questions or feedback, please leave a comment.
4 thoughts on “3 Modules That Help you Manage Contexts on Nodes”
I also developed Context entity field, this can be used to activate specific context using information stored in the current entity (eg.: activate context on node/user/taxonomy/… when the specified field have value X). This is really usefull when you need to change what you need to display (or location of items) related to editorial/user choises.
PS: I’m author
Thanks for the link, I didn’t see this module when I was writing this article.
I updated the article with a link to the module.
Once again, thanks. 🙂
If you use the “context_per_revision” branch on context_field you can have a context for each revision of an entity. This is very useful for revisioning the layout on the page especially when using the sps module (https://drupal.org/project/sps)
See a demo: https://drupal.org/node/1815788
Thanks for the tip. 🙂