0

The problem I have is that it seems that I can use global variable created inside another module, only if I use it inside callback function. I successfully tested it by inserting code inside existing modules.

The global variable is $subtotal.

function uc_checkout_pane_simple_quote($op, &$arg1, $arg2) {
    global $subtotal; 
drupal_set_message($subtotal);


function uc_webform_pane_checkout_pane_callback($nid, $op, &$arg1, $arg2) {
  global $subtotal; 
  drupal_set_message($subtotal);
}

In both cases the message shows the correct value.

However, inside other functions it is does not work.

function uc_webform_pane_checkout_pane_alter(&$panes) {
  global $subtotal; 
  drupal_set_message($subtotal);
}

function uc_cartdetails_checkout_pane_alter(&$panes) {
  global $user;
  global $subtotal; 
  drupal_set_message($subtotal);
}

What am I missing here?
What I am trrying to do: to show or hide checkout pane based on $subtotal value.

function uc_cartdetails_checkout_pane_alter(&$panes) {
global $subtotal; 
  foreach ($panes as &$pane) {

    if (($pane['id'] == 'webform_nid780') && ((40 - $subtotal) > 0) ) {
      $pane['enabled'] = FALSE;
      } 

    }
 } 

3 Answers 3

0

The problem is not the global variable, but the fact you are using a function like drupal_set_message() inside a alter hook, which is not necessarily invoked when Drupal is outputting a page.
If you want to debug a hook, it's better you go with watchdog() and check in admin/reports/dblog if there is any message. You need to enable the Database Logging or the Syslog module to see any output; in the latter case, you will see the output on the syslog of your computer, instead of admin/reports/dblog.

3
  • Hi, the result is the same. I found out that I can get the correct value even from this function "function uc_cartdetails_form_alter(&$form, $form_state, $form_id) {..." however from inside "function uc_simple_quote_checkout_pane_alter(&$panes) {..." I am not able to get anything. Possible that there are some arguments missing? I am really out of ideas.
    – loparr
    Oct 19, 2015 at 16:36
  • If you don't get anything from watchdog() then something else is wrong, but calling it from those hooks should work, supposing that those are really hooks. In the case you misspelled the hook names, then the code will not be executed, and you will not get any output.
    – apaderno
    Oct 19, 2015 at 16:42
  • everything seems ok. No spelling errors. I have one module (uc_cartdetails) with 2 functions, where calling global variable from both functions gives ok and zero result. Function uc_cartdetails_form_alter(&$form, $form_state, $form_id) {global $subtotal;watchdog('title',$subtotal); - gives result and function uc_cartdetails_checkout_pane_alter(&$panes) {global $subtotal; watchdog('title', $subtotal); - no result
    – loparr
    Oct 19, 2015 at 17:02
0

I'm not sure why globals don't behave well in Drupal, but it seems like what you're really asking is how to get global-like behaviour.

What you want is drupal_static. Your code should look something like:

function uc_webform_pane_checkout_pane_alter(&$panes) {
  //'uc_checkout_subtotal' can be whatever you like.
  //I assume '0' is a good default for this variable.
  $subtotal = &drupal_static('uc_checkout_subtotal', 0); 
  drupal_set_message($subtotal);
}

function uc_cartdetails_checkout_pane_alter(&$panes) {
  $subtotal = &drupal_static('uc_checkout_subtotal', 0); 
  drupal_set_message($subtotal);
}

...and so on, applying that same pattern wherever you need to write or read your global values.

0

Well, for whatever reason inside ubercart alter functions I was not able to use globals. I ended up with basic altering of form like this:

function hook_form_alter(&$form, $form_state, $form_id) {

 if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['webform_nid780'])) {
 global $subtotal;
 if ((40 - $subtotal) > 0)  {
 $form['panes']['webform_nid780']['#access'] = FALSE;  
    } 

 }
}

which works no problems.

Your Answer

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

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