Passing variables between different Drupal hooks using context API

Drupal’s nature of using hooks to allow functionality to be overridden by contributed modules makes it difficult to make data available between different hook implementations during the request process, especially if they are overridden. Simply put, we need to have certain values available system-wide once they are set.

For instance, Views block display defines an RSS link, but once the page template in the theme is reached that information is long gone. If we needed to have the RSS be the current page’s feed link, then we would require a complex (and sometimes risky) implementation. But the Context API offers a set of functions that makes life really easy in these situations: context_set, context_isset, and context_get.

First, figure out what information you need, from where, and very importantly, through what hook, if any, can you reach that variable. For example in a View, an attached feed url+icon can be obtained by using hook_views_pre_render.

It is important to determine if the value is already available or not. Then determine where the information is available, and if there is a hook to access that variable. In my case, I needed that information in the page template, or for that matter, the hook_page_preprocess function, because by the time the process reaches this stage the view will be long gone.

In your theme template.php file, your custom module, or feature, define a hook function at the moment your variable is available. For instance:

  1. <span><span><?php</span><br />
  2. <span>function </span><span>mytheme_views_pre_render</span><span>(&</span><span>$vars</span><span>) {<br />
  3.   switch (</span><span>$vars</span><span>-></span><span>name</span><span>){<br />
  4.     case </span><span>'my_view'</span><span>:<br />
  5.       if (isset(</span><span>$vars</span><span>-></span><span>feed_icon</span><span>) && !empty(</span><span>$vars</span><span>-></span><span>feed_icon</span><span>)){<br />
  6.       </span><span>context_set</span><span>(</span><span>'mytheme_page_feed'</span><span>, </span><span>'page_feed_icon'</span><span>, </span><span>$vars</span><span>-></span><span>feed_icon</span><span>);<br />
  7.     }<br />
  8.     break;<br />
  9.   }<br />
  10. }</span><br />
  11. <span>?></span></span>


The variable must be now set in the context you want, so we can to use it as needed. For instance, create a hook (if it doesn’t exist) or just add the extra code to an existing one. For example:


  1. <span><span><?php</span><br />
  2. <span>function </span><span>mytheme_preprocess_page</span><span>(&</span><span>$variables</span><span>) {<br />
  3.   if (</span><span>context_isset</span><span>(</span><span>'mytheme_page_feed'</span><span>, </span><span>'page_feed_icon'</span><span>)){<br />
  4.     </span><span>$variables</span><span>[</span><span>'page_feed_icon'</span><span>] = </span><span>context_get</span><span>(</span><span>'mytheme_page_feed'</span><span>, </span><span>'page_feed_icon'</span><span>));<br />
  5.   }<br />
  6. ::<br />
  7. ::<br />
  8. }</span><br />
  9. <span>?></span></span>


Now you can use the variable $page_feed_icon in your template page.
Notice the use of the hooks, and make sure they are used in the proper sequence. Also, the use of the functions: context_set, context_isset, and context_get, which:

See API.txt in the context module
Context provides a centralized set of API functions for setting and retrieving a static cache:


  1. // Set a static cache value at [my_namspace|my_namspace][mykey]<br />
  2. context_set('my_namespace', 'mykey', $value);<br />
  3. <br />
  4. // Retrieve a static cache value at [my_namespace|my_namespace][mykey]<br />
  5. context_get('my_namespace', 'mykey'); // $value<br />
  6. <br />
  7. // Boolean for whether there is a value at [my_namespace|my_namespace][mykey]<br />
  8. context_isset('my_namespace', 'mykey'); // TRUE

These are used internally by context, but may also be used by other modules. Don’t use the namespace `context` unless you want to affect things that context is up to.




An obvious but never overrated advantage is that you will have code that is more clean, less prone to errors, and less repetitive.


Felix Silberstein