Managing Regions and Blocks with Context

Regions and Blocks in Drupal

A typical Drupal page is built from static HTML, which defines layout's markup, and several regions of dynamic content. A good example of this approach is in The New Republic website:

The screenshot above shows the structure of the article detail page. We can see that article pages have four main regions:

  1. Banner Space
  2. Headlines
  3. Content
  4. Right Sidebar

Most regions (except the content region) do not by themselves define the content shown in the region. Rather they are containers for Blocks. Blocks in Drupal/Openpublish are small, arbitrary, dynamic portions of a page. More often than not blocks are provided by one of Drupal or OpenPublish modules, but a block can also be a piece of static HTML managed by an editor through an administration interface.

Beyond Core Drupal Block Management

If you have used Drupal, you are probably familiar with the core Drupal block management user-interface. It's very straightforward: you can add, manage and sort blocks in regions. Unfortunately, the interface is often too straightforward (read: limited). If you are building a complex CMS system like the one for an online publication, core block management is definitely not flexible enough.

Main limitation of the core interface comes from the fact that it is global: you can only manage blocks site-wide. There's no notion of a "section of a site". Everything you do applies to the entire website, which does not bode well with actual needs of Publishers. Publishers typically need to control regions' content (blocks) separately for various sections of a site. And that brings us to Context: a user-interface for managing sections (contexts) of a site and block configurations in those contexts (sections).

Working with Contexts

To access Context UI, go to : http://yourdomain.com/admin/build/context, where yourdomain.com is the root of your OpenPublish installation, or from the Admin menu go to: Administer > Site Building > Context. you should see a page that looks like:

As you can see there're a whole bunch of common context definitions that come pre-installed with OpenPublish and can be used for the variety of common use-cases. To get familiar with the system, let's create a new one. We are going to create a context for the detail page of a content type called "Page" (a simple, generic page).

Click on the "add" tab at the top right corner of the Context user interface. You should see new context creation screen:

First you need to enter three identifiers of the context:

  • Namespace - is the identifier of the system that context is used in. It helps avoid name clashes between context definitions produced by different Drupal distributions (e.g. OpenPublish and Tattler, or OpenPublish and OpenAtrium etc.). For our purposes this field is always set to "openpublish". You could set it to the name of your website, if you wanted to.
  • Attribute - is a group which you want your context to fall into. This is an important setting and usually requires some planning. It's important because contexts from different groups (having different "attribute" values) can overlap and exist concurrently. Contexts within one attribute don't get activated concurrently and only one of the contexts will actually get set, even if activation conditions of both contexts are met. This is why OpenPublish defines "sitewide" context in a separate attribute, called "general" apart from all other contexts. The sitewide context needs to "run" concurrently with any other context, therefore it may not share its attribute with any other context. For our new context let's re-use an existing attribute and set the attribute value to: "node_detail".
  • Value - is the actual identifier of the specific context we are creating. Let's set this to "page".

Final form should look something like:

Now that we fully identified our context, we need to tell OpenPublish/Drupal when is this context activated. Right below the form we just filled, there's a listing of available conditions. Please click on the condition that rads: "Node Pages." A list will appear to the right of the conditions list, scroll and find an item called "Page" and check the checkbox right next to it. Word "Page" will appear under "Node Pages" in the conditions listing:

So with the first three setting we defined the section of an OpenPublish website that gets activated (or: "context gets set" in Drupal terminology) and with the last setting we defined the condition that activates the context. The last piece of the puzzle is to tell OpenPublish what should it do if and when the context is set. To achieve this, we can either use one of the Reactions (set a theme variable, activate a specific menu item, or disable a region) and/or we can indicate which blocks to put in what regions.

To arrange blocks in regions, check a checkbox next to it and then click "add" link under the region which you want that block to get assigned to:

Once you have chosen blocks and regions and are done messing with the interface, save the form and make sure new context appears in the list of context. Then go ahead and create a new content item of "Page" type, go to its detail view page and make sure what you see in the sidebars matches what you would expect to see with the settings you used.

If you have not used Context in Drupal, before, it's good to spend some time playing with different settings to get a feel of how everything works. For further information about Context, you can also refer to its project page on Drupal.org.

Documentation

Loading widget...
No files to show
{{node.name}}
({{node.children.length}})
{{node.date}}
{{node.modified}}
{{node.filesize}}
{{node.filename}}