Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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).
Comments
Comment #1
quade CreditAttribution: quade commentedHere'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;
}
Comment #2
cntlscrut CreditAttribution: cntlscrut commentedI 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.
Comment #3
kenorb CreditAttribution: kenorb commented