I've tried creating own reaction on various conditions, but after spending whole day on it, it still doesn't work. Could anyone help me with it?

I've used code provided in API.txt, and context_reaction_debug.inc code (my module is attached (consists of 3 files)):

mymodule.module:

function mymodule_context_plugins() {
  var_dump(__FUNCTION__);
  $plugins = array();
  $plugins['mymodule_context_reaction_bar'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'mymodule'),
      'file' => 'mymodule_context_reaction_bar.inc',
      'class' => 'mymodule_context_reaction_bar',
      'parent' => 'context_reaction',
    ),
  );
  return $plugins;
}

function mymodule_context_registry() {
  return array(
    'reactions' => array(
      'bar' => array(
        'title' => t('Name of reaction'),
        'plugin' => 'mymodule_context_reaction_bar',
      ),
    ),
  );
}

mymodule_context_reaction_bar.inc:

// $Id: context_reaction_debug.inc,v 1.1.2.3 2010/08/04 23:43:04 yhahn Exp $

/**
 * Output context debug information.
 */
 var_dump(__FILE__);
class mymodule_context_reaction_bar extends context_reaction {
  function options_form($context) {
    return array('bar' => array('#type' => 'value', '#value' => TRUE));
  }

  function options_form_submit($values) {
    return array('bar' => 1);
  }

  /**
   * Output a list of active contexts.
   */
  function execute() {
    var_dump(__METHOD__);
    $contexts = context_active_contexts();
    foreach ($contexts as $context) {
      if (!empty($context->reactions['debug'])) {
        if (user_access('administer site configuration') && module_exists('context_ui')) {
          $name = l($context->name, "admin/build/context/list/{$context->name}", array('query' => 'destination=' . $_GET['q']));
        }
        else {
          $name = check_plain($context->name);
        }
        drupal_set_message(t("Active context: !name", array('!name' => $name)));
      }
    }
  }
}

After saving condition/reaction pair and triggering the condition (site wide / path) the "string(24) "mymodule_context_plugins"" is displayed once, and then disappears.

Any ideas?

CommentFileSizeAuthor
mymodule.zip1.3 KBger.lv
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RobLoach’s picture

Version: 6.x-3.0 » 7.x-3.x-dev

You can create your own reaction, but the reaction itself is never actually executed. Seems to be on Drupal 7 as well.

iamjon’s picture

It seems I have a similar problem http://drupal.org/node/1150770 Should I mark mine a dupe?

I'm using the almost the exact code in d.6 I've attached my reaction to a context but context_active_contexts() gets nothing.

It does get fired though.

Sorry if it's not a dupe.

iamjon’s picture

This maybe relevant http://drupal.org/node/966880
But I change the weight in my custom module via the db and still nothing.

RobLoach’s picture

Discovered what was missing in #1057894: Context Module Integration. The execute() function actually is never called. It's the module's responsibility to check when the Context is being used....

/**
 * Implements hook_context_page_reaction().
 */
function stringoverrides_context_page_reaction() {
  if ($plugin = context_get_plugin('reaction', 'stringoverrides')) {
    $plugin->execute();
  }
}
ericras’s picture

Component: Code » Documentation

Thanks Rob, I had this issue and using hook_context_page_reaction() as you suggest did the trick.

I think this is a Docs issue and hook_context_page_reaction() needs to be included in context.api.php and API.txt

febbraro’s picture

Status: Active » Closed (fixed)