Community Documentation

Updating contexts with block reactions when upgrading from Zen 6.x-1.x to 6.x-2.x

Last updated November 28, 2012. Created by rooby on November 28, 2012.
Log in to edit this page.

When you upgrade zen 6.x-1.x to 6.x-2.x some of the region names change.
If you are using the Context module, this will break contexts that add blocks to these regions.

This code snipped can be used to update the contexts to use the new region names.
It is for context 6.x-3.x.

Note that this will help in any situation where your theme regions change or you change themes. It doesn't have to just be for zen upgrades.

The easiest way to run this is to install the Devel module and go to the Execute PHP code page (/devel/php), then paste this code (without the PHP tags) in the text area and submit.

Make sure you back up your database first in case anything goes wrong and you have to revert.

<?php
/**
* Mapping of 1.x regions to 2.x regions.
*
* If you have custom regions you can add their mappings here.
* Some regions are commented out because they are either the same in both
* versions or they don't exist in one of the versions.
*/
$region_map = array(
 
'left' => 'sidebar_first',
 
'right' => 'sidebar_second',
 
'navbar' => 'navigation',
//  '' => 'highlight',
//  'content_top' => 'content_top',
//  'content_bottom' => 'content_bottom',
//  'header' => 'header',
//  'footer' => 'footer',
 
'closure_region' => 'page_closure',
);

/**
* Update the regions that blocks are assigned to in the contexts.
*/
$contexts = context_load();
$count = 0;
foreach (
$contexts as $context) {
 
$changed = FALSE;
  if (isset(
$context->reactions['block']['blocks'])) {
    foreach (
$context->reactions['block']['blocks'] as &$block) {
      if (isset(
$region_map[$block['region']])) {
       
$block['region'] = $region_map[$block['region']];
       
$changed = TRUE;
      }
    }
  }
  if (
$changed) {
   
context_save($context);
   
$count++;
  }
}
drupal_set_message(t('@count contexts have been updated.', array('@count' => $count)));
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Level
Beginner
Audience
Programmers, Site builders
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here