Context module as an alternative to manage widgets and secondary content in Drupal

In Drupal, the most popular way to manage widgets, menus and other pieces of information that are placed in the website’s template regions is through Drupal blocks, which comes with the Drupal core. In small projects, the blocks module ist an efficient way to have these elements placed.  However, when a website becomes more complex and bigger, this module is less efficient because:

  • As the amount of blocks on the website increases, it is harder to find them in the interface. Also, it is really hard to figure out which blocks are visible on one specific section.
  • If the number of website sections increases, the visibility rules might become more complex and probably, we need to set PHP code to achieve the requirements of visibility.
  • Blocks module does not allow to place an element in two different regions. Therefore, in order to have the same information in multiple regions you end duplicating blocks, which impacts the performance of the website.

The Context module is a way to extend and enhance the administration of Drupal blocks. In this post  we are going to show how Context works and how to use it to place blocks elements on a Drupal project.

Relationship between Conditions and Reactions

Context is all about conditions and reactions. To make that topic easier, we can see this as a IF…THEN statement. The conditions are the boolean values of the IF clause and the reactions are the actions to be taken if the IF evaluation is TRUE.

Install and enable Context module

To Install the module you need:

Once, you have the modules on your server, enable the modules context, context UI and Chaos Tools.

Create a new Context

  • Go to Structure > Context  and click on Add.
  • Notice that there are some fields that needs to be configured:
    • Name: The name of the context, this is the ID and has to be in lowercase without spaces.
    • Tag:  A category to organize and group the contexts.  A good idea to get an organized environment is create tags by scope, e.g. blog, profile, sitewide, global.
    • Description: An human readable name. The value of this field will be displayed on the Context list instead of  the Name field.
    • Require all Conditions: If checked, all conditions must be true for this context to be triggered. Otherwise, the first condition that is met will activate this context.
    • Conditions: Are the rules that activate and trigger the reactions of the context
    • Reactions: The actions that will be triggered when the conditions are TRUE.

Set the Conditions

  • Click on the dropwdown list  within conditions section. The most popular conditions are:
    • Path: Related to the current Drupal Path.
    • Sitewide Context: Ideal for a group of blocks that always need to be rendered.
    • Taxonomy Term: The condition is TRUE if the node has a relationship with the taxonomy terms selected for this condition.
    • User Role: Ideal for blocks that only need to be displayed for specific user roles.
    • User Page: Evaluate if the current page is a user settings page.
    • Node Type:  Ideal to set blocks that belongs to a content type.

There are more conditions available provided by this and other third party modules. Context module has an API to create new conditions and reactions.

  • Fill the fields related to the option selected above, with the additonal info that the module needs to evaluate the condition.
  • You can add as much conditions as you need for a context.

Set the Reactions

  • Click on the dropdown list and select Blocks. There are additional options to set a custom breadcrumb,  set HTML classes, Set/Unset regions. However, for block placing purposes We need to select blocks option.
  • When Blocks is selected, notice that the entire list of blocks appears on the right and all the theme regions on the center.
  • Please check the  block you desire to place.
  • Then, Click  Add link  on the region you want the block.
  • Notice that  the selected block is listed under the region you selected. The region can have an unlimited number of blocks and might be sorted dragging and dropping the blocks.
  • Save the changes
  • Finally, go to a node where the conditions for the configured context are TRUE  and notice that the block is displayed on the selected region.

As you can see use this module improves the way on how a block is displayed. It is necessary to mention that this module allows to set an instance of a block  in different regions avoiding the blocks cloning.  Keep in mind that context is more that block placing, you can configure reactions to change breadcrumbs, themes, selected menu items and also, It can be extended to some reactions offered by other contributed modules.

Do you need some help?

Let's get started. Tell us a little about yourself and your organization
and we can start a conversation about your needs and our capabilities.

Related Posts