How to pass a variable from one function to another function?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
jessSchn's picture

On a Drupal 6 Website I am trying to accomplish the following:

On a node (page), I want to pull the node title and then I want to send the node title over to a webform that is displayed as a block on the same page, as a hidden field.

Here is what I have so far:

//currently being done inside a custom module - not sure how else to code it

//get node information
function customform_nodeapi(&$node, $op, $teaser, $page){
   switch($op){
       case 'view':
         if ($node->type == "recorded_webinars"){
              $cf_node_title = $node->title;['value'];
          }
      break;
}//end switch  
}

//place node information in hidden field on form block
function customform_form_webform_client_form_9232_alter($form, &$form_state,$form_id) {// on-demand webinar request form.

  $form['webinar_title'] = array(
      '#title' => t('On Demand Webinar Title'),
   '#type' => 'hidden',
    '#value' => $cf_node_title
  );
  }

To my understanding - the hook_nodeapi function works. The only thing not working on the hook_form_alter() is the value.

How do I pass the $cf_node_title value over to the hook_form_alter() function?

Comments

In function

JBack's picture

In function customform_form_webform_client_form_9232_alter

//place node information in hidden field on form block
function customform_form_webform_client_form_9232_alter($form, &$form_state,$form_id) {

  // on-demand webinar request form.
  $form['webinar_title'] = array(
    '#title' => t('On Demand Webinar Title'),
    '#type' => 'hidden',
    '#value' => $node->title
  );
}

The $node object is "global", meaning all functions can see it.

tried that...

jessSchn's picture

Thanks for the response, but I had already tried that and my value wasn't getting filled in - it was just blank.

Whoops!

JBack's picture

Sorry,
didn't realize you were using form_alter, and that the $node in your previous function was being passed in.

This is a good use case for devel, where kpr($form) may help you out.
Regardless, I did a little googling, and came up with this thread...

http://drupal.org/node/183872
This is how they got it...

$node = $form['#node'];
$title= $node->title;

Hope that helps, sorry for the runaround.

Close but no cigar :)

jessSchn's picture

So I gave the code you provided a try and I did get the hidden value set. But - the hidden value had the title of the webform (that is displayed as a block), I'm trying to get the Page title (that displays the block) into the hidden value - not the webform title. :/

Solved!

jessSchn's picture

Here is what I ended up doing in my module to get the page title sent to the webform block:

function customform_nodeapi(&$node, $op, $teaser, $page){
    global $cf_node_title;

   switch($op){
       case 'view':
         if ($node->type == "recorded_webinars"){
              $cf_node_title = $node->title;
          }
      break;
}//end switch
     
}

//place node information in hidden fields on form block
function customform_form_webform_client_form_9232_alter($form, &$form_state,$form_id) {

global $cf_node_title;

  $form['webinar_title'] = array(
   '#title' => t('On Demand Webinar Title'),
   '#type' => 'hidden',
    '#value' => $cf_node_title
  );

}

Twin Cities

Group events

Add to calendar

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: