Converting your site from Core Blocks to Context

Submitted by Brandon Cone on 05/30/2013 - 10:02:am

We often times get clients who want us to deploy a homepage revamp or new feature to their site.  In doing this, we will often work to move a site's blocks from the core block module and into context.  We will then use the Features module to deploy the updates to the clients site.  The goal is always to provide a "one-click" deploy to our clients and features gets us a long way toward that goal.

There is one issue that arises when moving blocks out of core block administration and into context, however.  When the feature is enabled there will be duplicate blocks on all pages affected by the context until we are able to remove them from core blocks.  This is not a huge encumberance but can be time consuming when there is a high number of blocks to disable and it is divergent from the goal of a "one-click" deploy.

To solve this and provide ourselves the ability to deploy we implemented an install hook in our feature to disable the blocks from core automatically.

  1. include_once('my_feature.context.inc');
  2.  
  3. /*
  4.  * Implementation of hook_install().
  5.  *
  6.  * Disable the blocks from core blocks which we are now managing through context.
  7.  */
  8. function my_feature_install() {
  9.   // Get the contexts we're using
  10.   $contexts = my_feature_context_default_contexts();
  11.   // Iterate through each context's blocks and make sure that that block is
  12.   // disabled on the core block page.
  13.   foreach ($contexts as $context) {
  14.     if (isset($context->reactions['block'])) {
  15.       foreach ($context->reactions['block']['blocks'] as $block_name => $block) {
  16.         db_update('blocks', 'b')
  17.           ->fields(array(
  18.             'status' => 0,
  19.             'region' => '',
  20.           ))
  21.           ->condition('module', $block['module'])
  22.           ->condition('delta', $block['delta'])
  23.           ->execute();
  24.       }
  25.     }
  26.   }
  27. }

Happy coding!

blog photo provided by karimian