I have 2 hook functions in my module: hook_views_pre_render() and hook_form_alter(); in hook_views_pre_render(), I retrieve some data about what nodes that are available that I need in hook_form_alter().

I'm looking for a way to pass the data from hook_views_pre_render() to hook_form_alter(). It seems simple, but I can't get it to work. Does anyone any idea?

What works for the moment is to add global $aVariable to both the hook functions, but I believe there should be a better way.

3 Answers 3


you can use variable_set($name, $value) and variable_get($name, $default = NULL)

  • 1
    If the data changes page to page, then this approach will fail weirdly when two users view the same page.
    – mpdonadio
    Aug 17, 2011 at 13:33
  • i didn't end up using this since the form_alter is called before the views_pre_render so the data i want to share isn't even there. But in concept using the variable_set and get works, but so does the global.
    – dazz
    Aug 17, 2011 at 14:36
  • This solution only works if your website only has one user. Who uses Drupal for single-user websites? Also, it is slow. Jul 3, 2015 at 11:02
  • @rudolfbyker You can say it works for a single value, not for a single user. And "single value" is debatable because you can use serialised data... And it's slow compared to what?
    – Stefan
    Jul 5, 2015 at 17:54
  • 1. See MPD's first comment. It's because variable_set does not set a unique variable per user, but writes to the database, causing the variable to change for all users currently hitting your site. 2. Slow compared to any solution that does not hit the database. Jul 7, 2015 at 4:24

In Drupal 7, drupal_static() could be used too, if the stored value doesn't need to be persistent between different pages.

drupal_static() is normally called as drupal_static(__FUNCTION__, $default_value), but if you use the name of the function, you can access the same value accessed from the function that calls drupal_static().

Using variable_set() and variable_get() the value is persistent between different pages, and it is not automatically removed. If the first hook execute code similar to the following one, then the second module would get an older value.

if ($condition) {
  variable_set($variable_name, $value);

Rather than using such code, the first hook should execute code similar to the following one.

if ($condition) {
  variable_set($variable_name, $value);
else {

Whatever method you choose, be sure the hook reading the value is effectively running after the hook setting that value.

  • You mention drupal_static can be used to pass values between hook_functions. Can you please update your answer to show a clear example of using drupal_static to set the value in one function, and then call that data in a separate function?
    – blue928
    Apr 10, 2013 at 4:19
  • Also, could you explain more on how to ensure variable set is firing before variable get? Is there a way to give "hook fire precedence" or weight to hooks so they fire in a preferred order?
    – blue928
    Apr 10, 2013 at 4:20
  • @blue928 No, there isn't a way to let Drupal call a hook first a different hook; there is just a way to order different implementations of the same hook.
    – apaderno
    May 9, 2014 at 18:27
  • In both the cases, the hooks call drupal_static() as &drupal_static($value_name, $default_value). The function setting the value normally uses __FUNCTION__ as $value_name, but that is not necessary; as long as the function doesn't create conflicts with other functions, any value is good, even though prefixing it with __FUNCTION__ helps.
    – apaderno
    May 9, 2014 at 18:30

If you need to pass information that is only available in hook_views_pre_render to hook_form_alter a global variable might be your best option; preferably named to make it's restricted role clear, e.g., _mymodule_available_nodes.

The best approach would be to add your data to any objects the two calls have in common. In this case unfortunately there there appear to be none in common, so this won't work.

You might be able to use session data, but that would be overkill for this application, and doesn't seem to offer clear benefits over a global.

I don't recommend using variable_set/variable_get. variable_set clears the variable cache on every call, plus all variable values are fetched on every page request. This is not a good match for data that is constantly changing, and of potentially large size.

  • In Drupal 7, can drupal_static() be used for this?
    – kalabro
    Aug 17, 2011 at 11:50
  • A static variable is local to the function but retains the value between calls. The OP wants a way to communicate between functions so this doesn't apply. drupal_static provides a method for storing static data that can be reset and manipulated from outside the defining function.
    – keithm
    Aug 17, 2011 at 11:59
  • 2
    The variable that is returned by drupal_static() is local to drupal_static(), but the function returns the value of the variable by reference; this means the calling function is able to change the variable's value even if it is a static local variable for drupal_static().
    – apaderno
    Aug 17, 2011 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.