Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to pass a global array and work with it in hook_field_presave() but that global array is unset. How do I pass a global array and work with it in presave?


function find_feed_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
global $testvar;
$testvar = $array = array(
    "foo" => "bar",
    "bar" => "foo",

function find_feed_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
   global $testvar;
   dpm ($tesvar);  // result = NULL
share|improve this question
From purely PHP point of view what you do makes sense, BUT be aware that not all hook calls happen in the same http request. Specifically creation of a widget and submit handling from said widget cannot happen in one call, so there is no way global variables set in one will be visible in the other. So the answer to "how do I pass a global array?" is simply you don't. Now, if you would tell us what this array is, logically, what is it's role in your program, we could point you in the right direction. – Mołot Apr 18 '14 at 9:05
I am using simplepie as a function to find RSS feeds. When called upon it will fill in my custom field-names with the nessecary data, like URL/TITLE/Description etc ect. However, the array that simplepie returns contains a lot more info which i need to work with during presave – user1973842 Apr 18 '14 at 9:07
up vote 3 down vote accepted

This will never work - global variables only exist in the context of a single page request. Since the form build and presave happen in different requests, you can't use globals in the way you're trying to.

Use a session var instead, that will persist

share|improve this answer
Session var is also unset, I have tried that. – user1973842 Apr 18 '14 at 9:46
If your session vars aren't persisting then you have a serious problem with your app/server. I doubt it though, you wouldn't be able to login to the site if that were true – Clive Apr 18 '14 at 9:48
if I set $_session['my_var'] = some array(); in my callback function or during widget creation, it will be gone when function hook_presave() is called. – user1973842 Apr 18 '14 at 9:50
Yeah, the global session var is all caps - $_SESSION. You're setting the wrong thing – Clive Apr 18 '14 at 9:51
yes, I used $_SESSION :) trust me, it is unset. – user1973842 Apr 18 '14 at 9:56

The "drupal way" would be to serialize the value you want to keep, and save it with :

variable_set('name_of_your_variable', serialize($your_array));

Then get the value with :

variable_get('name_of_your_variable', 'default_value');

But if the variable is user-related, it will obviously not work and you will have to use the sessions, as indicated above.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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