Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
3rd August 2013 - 5 minutes read time
Context is a Drupal module that allows you to set up reactions that fire when certain conditions are met. This might be when a certain path is loaded, or when a particular content type page is viewed, or even on every page on a site. When the conditions are met a number of reactions can be fired, which include placing blocks, setting breadcrumbs, or just adding a class to the page template.
I use the Context module extensively in my projects as they provide a better way of controlling the placement of blocks than the default Drupal blocks admin pages. One of the most important differences is that Context allows for the same block to be placed into two different regions on a site, which is something Drupal can't do out of the box. More importantly, Contexts are exportable and fully integrate into Features. This means that I can setup a collection of Contexts on my development environment and package them into a Feature that I can then deploy to the live site and not have to manually setup the placements of blocks.
I often find myself wanting to get Drupal to react to something that I have already set up as a Context but which Context doesn't have a reaction for. Rather than integrate with the Context API directly and create a reaction plugin I will often just detect that the Context has been activated and act accordingly. This is to do things like altering theme templates suggestions or to tweaking classes on certain components and so creating a reaction plugin isn't necessary. It is also often easier to set up a Context for a particular situation without having to write lots of code to detect it. All of the detection mechanisms within Context are well tested and have normally been run so there is little point repeating functionality.
To use an existing Context in your code you need to call the function context_active_contexts(). From this you will get an array of Contexts that have been activated for that page. The keys of this array are the machine names of the active Contexts so a quick inspection of the array keys can show if the Context you are interested in is active. From here you can then do whatever you need to do.
The following example grabs the active Contexts and looks to see the 'news' Context is active. If it is then the page--news.tpl.php template is used to render the page. The Context in question is checking for more than the content type here, it is also looking at a series of news views that show the news in different ways.
function MYTHEME_preprocess_page(&$vars) {
$active_contexts = context_active_contexts();
if (in_array('news', array_keys($active_contexts))) {
$vars['theme_hook_suggestions'] = array('page__news');
}
}
In order to achieve the same effect without using Context the following code would be needed. This way of detecting the current content of the page is (in my opinion) slightly more complex to understand. Also, since Context has already done all the work in detecting the correct page then we are largely repeating that functionality here.
Drupal's path system gives authors the ability to override the path of an item of content. Whilst the internal path of a page might be "/node/1" the external path can be anything you want, and can be set when editing the item of content.
I've been doing a bit of Drupal migration recently, and one of the tasks I have undertaken is to migrate "likes" from a Drupal 7 site to a Drupal 10 site.
Context definitions in Drupal are really useful when you want to inject context into your plugins and I have been using them for a little while now. It has made building custom blocks to add content to pages much easier as I don't need to copy the same block of code to find an entity of a particular sort into the block plugin.
I previously looked at injecting context providers into context aware plugins. This time I will explore more about creating our own context providers to plug into this system.
Add new comment