It would be really nice to be able to remove a single reaction from a context, say, a block, in a preprocess function.

Basically, mimicking block visibility but within the framework of context.

Is this possible, or must one set up multiple contexts?

If this has been already answered/documented, apologies. I've looked in vain.

Comments

glass.dimly’s picture

Status: Active » Closed (fixed)

Found my answer in the API.

<?php
/**
* Implementation of hook_context_load_alter().
*/
function custom_module_context_load_alter(&$context) {
  if ($context->name == 'context_name') {
    if (arg(0) == "view_path") //on friends view now
    dpm($context->reactions['block']['blocks']); //find your block name
    unset($context->reactions['block']['blocks']['block_name']); //unset block condition
  }
}
?>
tonytosta’s picture

Thanks for posting your solution! :)

doublejosh’s picture

While this is cool, it also seems like a horrible idea.

It's been 6 months and I imagine by now someone has spent hours fighting this context, reverting the feature, and pulling their hair out trying to figure out why this reaction isn't acting like it's suppose to :)

elusivemind’s picture

Issue summary: View changes

The best way to do this is in an update hook. As an example - in one hook I had to removed a path that contained a mis-capitalized word and also remove a reaction that was applicable to the context. I did so with the following:

<?php
function mymodule_update7101() {
 
  $result = db_select('context', 'c')
    ->fields('c')
    ->condition('name', 'machine_name_of_context_to_edit', '=')
    ->execute()
    ->fetchAssoc();
  $conditions = unserialize($result['conditions']);
  $reactions = unserialize($result['reactions']);
  
  unset($reactions['block']['blocks']['block_name_to_remove']);
  unset($conditions['path']['values']['condition_path_url_to_remove']);
  $conditions['path']['values']['condition_path_url_to_set'] = 'condition_path_url_to_set';
  
  db_update('context')
    ->fields([
      'conditions' => serialize($conditions),
      'reactions' => serialize($reactions),
    ])
    ->condition('name', 'machine_name_of_context_to_edit', '=')
    ->execute();
}
?>