Skip to content Skip to navigation

Using Context to Add Blocks to a Region

You may be asking yourself, how do I add a block to a sidebar or other region, and how do I get it to display on the correct page? In this post I'll explain how to use the Context module to do just this.

Blocks are a great way to display content in a more structured way on your page. A block is a may be created with a view, as bean, or a simple block. By placing blocks in regions, you can get different layouts. If you want a sidebar or other region to display on your site, you simply need to add a block to the desired region (provided by your theme) and that region will appear once it has content inside of it. If your site is on Stanford Sites your site will have regions such as first (left) and second (right) sidebar.

Drupal almost always gives you several approaches for anything you do. Putting a block in a region is one of those cases. One approach is to use the Blocks module at Structure > Blocks; a second approach is to use the Context module at Structure > Context. At Stanford Web Services, we prefer to define contexts for placing blocks because they provide more flexibility and functionality than what we get on through the Blocks module.

How Context Works

Context uses an “If ... then” approach to placing blocks.  The Conditions are the “If,” and the Reactions are the “Then.” So, for example, if a condition, such a URL path for a page, is met, then the context will display specific blocks in designated regions on that page.  

Using Context, you can designate a whole set of blocks to display, for example, on any events related page. To do this you might define all events-related pages with a common base URL such as:

URL

Page description

https://mysite.edu/events

Landing page for events

https://mysite.edu/events/my-event

Page for a specific event

Then you’ll create a context at Structure > Context which will have as one of it’s conditions a path.  The path would be designated as:

Path

Description

events

Landing page for events

events/*

All individual event pages

The Reactions, for this example, would be to designate a block or set of blocks to display in one or more regions, such as the sidebar, on the events-related pages.

Using Contexts to Add Blocks to Regions

Check Your Setup

  1. To verify that both the Context and Context UI modules are enabled, navigate to the modules page.  From the black admin menu, select Modules.

  2. If not checked, check the boxes for both Context and Context UI modules.

  3. To verify you have the permissions to administer contexts, from the black admin menu, navigate to the permissions page at:
    People > Permissions

  4. If not checked, check the box that gives your role the Administer contexts permission.

Create a Context

  1. Navigate to the context page from the black admin menu:
    Structure > Context

  2. Click on + Add.
    This will bring up the Add a new context page

  3. Enter a name for your context. This must be computer readable so you’ll use lowercase letters, dashes or underscores, and no spaces.

  4. Enter a tag. This optional tag is used to group similar contexts on the Context UI page.

  5. Enter a description. This optional field allows you to describe a context in human readable terms.   

  6. To add conditions for your context, click on the <Add a condition> drop down, and select Path.  

  7. This will bring up a text edit field. Enter paths, one per line, that describe where you want the context to be effective.  For this example enter:
    events
    events/*

Add Reactions (Blocks) to Your Context

  1. To add reactions, click on the <Add a reaction> dropdown menu

  2. Select Blocks.

  3. This will bring up a long form listing all the regions available on your site.  In the column to the right of this list, about halfway down the page, is a scrollable list of blocks. Use your mouse to scroll through this list and select the blocks you wish to place in a single region.

  4. In the center column, click on the +Add for the region in which you want to place the blocks, and the blocks should appear in the list for that region.

  5. Drag the blocks into correct order with the +. (Note: experience has shown that even if the blocks initially appear in the correct order, upon saving, they may not stay. To pre-empt this, simply move them around and back into the correct order before saving.)

  6. Once you’ve finished putting blocks into regions, click on Save.

  7. Verify your work by navigating to a page described by the conditions in the context.

Categories: