I'm using the migrate module to move content into a Drupal site. Some of this content needs to be created as blocks and added to a region using contexts. I have the creation of blocks (actually, boxes) working using a purpose-built module, but I don't know where to begin on the context side of things - looking for something like add_block_to_context() but I cannot find anything. Any ideas?


After checking the code used in the context test units for Drupal 6:

$context = ctools_export_new_object('context');
$context->name = 'testcontext';
$context->conditions = array('path' => array('values' => array('admin')));
$context->reactions = array('debug' => array('debug' => TRUE));
$saved = context_save($context);

You can easily code your reactions or conditions then save the context.

BTW to get all context you can use context_context_list().


The easiest way to do this, is to actually create the full context with all blocks first and then export it, using the built in export option or features.

  • I'm talking hundreds of blocks here, so it would be much better if I can automate this – Bobby Jack Oct 5 '11 at 10:59

I have fixed this issue using below codes.

Before that use context to export all values to check blocks array format or some structure.

$context = new stdClass();
$context->disabled = FALSE;
$context->api_version = 3;
$context->name = 'name-of-context';
$context->description = 'Description';
$context->tag = '';
$context->conditions = array(
'path' => array(
  'values' => array(
    'node/120' => 'node/120',
$context->reactions = array(
'block' => array(
  'blocks' => call_function_to_generate_block_array(),
'theme_html' => array(
  'class' => 'myclass',
$context->condition_mode = 0;

Note: you can save all these values into one script file & can usedrush scr script.php command to execute it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.