Context: Changing how to Code in Drupal – Liveblogging from Drupal Camp NYC 8

By Seth Cohn (sethcohn)

Get the Context module here: http://drupal.org/project/context
Another great module suggestion: http://drupal.org/project/menu_block (not necessarily related to Context though)

What’s the idea of context

Context pays attention to what’s happening on the page while the page is being rendered. Developers used to take arguments from the final URL path, but Context is saving a lot of information (such as type, nid, uid etc.) for later use (in the page load process). This is pretty awesome, because all content (such as the full node object) is already available in the Context. In short words: Smart global variables that persist throughout the whole (Drupal) page load.

The original description from the module page: Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a “section” of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Basic construct of a context: Add a block using Context

  • Conditions: When should this context take effect?
    Example: This context should be active any time I look at a node with the type “page”, but not on the edit form
  • Reactions: What should happen?
    Example: Show a block in a specifc region
  • Et Voila: Never use the blocks interface again

Another Example using Context Reaction: Theme

  • Condition: You’re looking at a node type “page”
  • Add a specific body class to this page

Other Conditions

  • Node tagged with a specific item
  • Looking at a specific path (including wildcards)
  • Set this context for a specific user role
  • Show this context for specific views
  • All conditions can be combined with each other and can be used with either AND or OR

Other Module Integrations: