To reproduce I created a simple module with the following code (this is the whole .module file):

function mymodule_init() {
  // Set a sitewide "node" ontext if we are on a node page
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    context_set('node', 'object', $node);
    context_set('node', 'form', arg(2) == 'edit');
  }
}

Then in my template.php I have this:

$x = context_get('node');
dpm($x);

While my context inspector shows the "node" context being created properly, the dpm() in my template.php returns an empty sting (or FALSE)

Comments

bleen’s picture

to clarify, the code in template.php is within function mymodule_process_page()

lucascaro’s picture

The same happens to me with 7.x-3.0-beta2

I have two contexts active (I've added debug to the reactions and I can see the messages for both) but context_get and context_active_contexts return empty when I'm in a .tl.php other than page.tpl.php and html.tpl.php
Also, the inspector is not showing (at all).

It seems that the contexts get activated, and execute the reactions, but I can't see them from the theme layer (and neither does the inspector block).

Jason Dean’s picture

Same here in 7.x-3.0-beta3

context_get() and context_active_contexts() only return values when I use in theme_preprocess_page()

If I try to use them in theme_preprocess_node() or elsewhere, they return empty.

Jason Dean’s picture

In a custom module, calling this function lists all enabled contexts:

function MYMODULE_check_context() {
$active_context = context_enabled_contexts();
kpr($active_context) ;
}

But this lists nothing:

function MYMODULE_check_context() {
$active_context = context_active_contexts();
kpr($active_context) ;
}
function MYMODULE_check_context() {
$active_context = context_get();
kpr($active_context) ;
}

Perhaps it depends how late in the page load these functions are called? I'm trying to use it in a Panels visibility rule, and maybe it is calling before the active contexts are set.

dddbbb’s picture

@pushka - Did you ever get anywhere with this? I'm trying to do something similar (detect context from Panels visibility rule) but it's not working for me.

matt2000’s picture

Category: bug » support

me too. What's up?

bleen’s picture

Category: support » bug

This is still a bug ...

ruuizz’s picture

There is a comment on context module that indicates to call the function as late in the page load as possible. I changed the execution of my code to the end of the page load and I was able to get all the active contexts with no problem

From Context Module:

/**
 * Loads any active contexts with associated reactions. This should be run
 * at a late stage of the page load to ensure that relevant contexts have been set.
 */
function context_active_contexts() {
  $contexts = context_get('context');
  return !empty($contexts) && is_array($contexts) ? $contexts : array();
}
tekante’s picture

Bleen, I am unable to replicate your issue using your originally provided code along with the update in comment #1. When trying to do a dpm in a MODULE_process_page function the output doesn't show until the next page load. When called from within MODULE_preprocess_page, however, it should show on the current load. Irrespective of when the dpm output shows, the actual return should be correct. If the code is outside of a function and just within the top level of template.php as your original submission seemed to indicate then a false return would be expected bacause $x would be defined before the hook_init function would run.

Can you provide any additional information?

For those looking at context_active_contexts, @ruuizz is correct about calling that as late as possible to ensure all contexts are activated which is done during various hooks depending on the conditions that activate them.

tekante’s picture

Status: Active » Postponed (maintainer needs more info)