I'd like to suggest a new hook (hook_active_contexts_alter) for Context to allow for a last-minute alter of what contexts are considered active.

The idea is that you could do some last minute changes to the contexts that are considered active. For instance, if you have a site that has multiple domains (a country code, or a m.* mobile site), you could choose to disable certain contexts. In my case, a mobile site (same site, different domain) switches themes for a mobile site. With this, I can choose to disable certain contexts while changing others (b/c the theme regions might be different).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quade’s picture

Here's a simple patch for my proposed hook.

In my case, I could have (in a custom module for my site) an implementation of that hook, like:
function MYMODULE_MOBILE_active_contexts_alter($contexts) {
foreach ($contexts as $name => $context) {
if (some_helper_function('some_variable', FALSE) && strtolower($context->tag) != 'mobile') {
unset($contexts[$name]);
}
}
return $contexts;
}

cntlscrut’s picture

I noticed that if there is no instance of the alter hook to be invoked, because the $contexts var can't be passed by reference, $contexts is completely unset which kills all contexts.

Maybe an addition of a self-called default instance could be implemented to maintain the existing values like:
function context_active_contexts_alter($contexts) {
return $contexts;
}

I did think of maybe an implementation of drupal_alter, but, I think that may be too expensive to be used in this case.

kenorb’s picture

Issue summary: View changes
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: context-add_active_contexts_alter-1642358-1.patch, failed testing.

The last submitted patch, 1: context-add_active_contexts_alter-1642358-1.patch, failed testing.

The last submitted patch, 1: context-add_active_contexts_alter-1642358-1.patch, failed testing.

The last submitted patch, 1: context-add_active_contexts_alter-1642358-1.patch, failed testing.