1

Drupal 7 I'm having a similar problem to one that's been presented previously but so far I've not been able to make any of the suggestions work. I have 'Product' pages of content type 'Software Products'. I want to place a link on the product pages pointing to a Webform 'Request Information' I want to populate a (hidden) field on the form with the product name which is also the title of the referring product page. I have tried the following but this just results in the title of the form being shown - not the referring page.

<?php  
/** 
*  Implementation of hook_form_alter(). 
*/  
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {  
  switch($form_id) {  
       case 'webform_client_form_10': // the id of the form  
               {$current_object = menu_get_object(); 
               $product_title = $current_object->title; 
               $form['submitted']['product']['#default_value'] = $product_title; }
           return $form;
       }

} I would appreciate any pointers - I'm new to Drupal

rdans
  • 2,179
  • 22
  • 32
lshore
  • 11
  • 1
  • 3

1 Answers1

1

That's quite a messy way round of doing what you need to, you should just put the product nid in the URL as part of the query string in the link from your product page and then load it up from the webform.

In your node template/preprocess:

$webform_path = 'node/10'; // Or whatever the webform's nid is
$link = l('Request Information', $webform_path, array(
  'query' => array(
    'product_nid' => $product_node->nid
  )
));
echo $link;

Then in your form alter:

function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {  
  if ($form_id == 'webform_client_form_10' && isset($_GET['product_nid']) && is_numeric($_GET['product_nid'])) {
    $product_node = node_load($_GET['product_nid']);
    if ($product_node) {
      $product_title = $product_node->title; 
      $form['submitted']['product']['#default_value'] = $product_title;
    }
  }
}

Note that you don't return the form from the hook_form_alter function, the $form variable is passed in by reference so changes are stored that way.

Clive
  • 36,918
  • 8
  • 87
  • 113