Webform 6x 3.11 - custom code to determine value of webform field I intend to pass a predetermined NodeID into a webform through URL querystring like so:

www.mysite.com/forms/my-form?nid=43


On the webform, one of the field values must be calculated by getting the Node Title based on what the nid is. I want to use a textfield, so that it can be one of the values submitted in the email. Can this be achieved?...because from what I see, the "default value" box only allows for a few tokens and no custom code. Furthermore I don't want to pass in the NodeTitle directly in the querystring for 2 reasons:


It will contain spaces and therefore will not be  URL encoded, and
I want to avoid people tampering with the querysting


Here's hoping some drupal guru can come to my rescue!  thanks in advance!
We had exactly the same issue when we were developing a custom job application online form, and we wanted to pass in the job details into the webform in question. How we solved it:


Created (job application) webform node
Created links to the webform node above in the form of /apply-online?n=17
Created custom node-856.tpl.php (856 happened to be the node of the apply online webform) to 

first validate the nid as a valid job
validate that the job is published
validate that the job had not closed
format the additional fields into the webform such as title, position etc etc
stop any URL tampering

Created a custom webform validation hook to ensure that only published nodes of type 'job' were allowed to ever be submitted (to stop any form tampering)


Sample code for the validation hook:

/**
 *  Implementation of hook_form_alter()
 *  This function is used to add an extra validation function to the apply online webform
 */
function site_profile_form_webform_client_form_856_alter(&amp;$form, &amp;$form_state)  {
  $form['#validate'][] = 'site_profile_validate';
}

/*
 *  Implementation of hook_validate()
 *  Doesn't exactly validate the webform as that's done in another validation function
 *  This takes the nid from the job_url field and turns it into a url.
 */
function site_profile_validate($form, &amp;$form_state) {
  global $base_url;

  $nid = $form_state['values']['submitted']['job_url'];
  $node = node_load($nid);
  $job_url = $base_url . url('node/' . $nid);
  $form_state['values']['submitted']['job_url'] = $job_url;

  //need this to stop the form losing the nid argument in the url
  if (empty($form_state['values']['submitted']['cv']['_fid']) || empty($form_state['values']['submitted']['cover_letter']['_fid']) || empty($form_state['values']['submitted']['completed_application_form']['_fid'])) {
    drupal_goto('apply-online', array('n' =&gt; $nid));
  }
}


Sample code for the node-856.tpl.php:

  // job's apply online node ID = 856, this template was made to theme this
  // particular page

  $job_nid = $_GET['n'];
  if (!is_numeric($job_nid)) {
   drupal_access_denied();
   exit;
  }
  $job_node = node_load($job_nid);
  if ($job_node-&gt;type != 'job') {
    drupal_access_denied();
    exit;
  }
&lt;div id="node-&lt;?php print $node-&gt;nid; ?&gt;" class="node&lt;?php if (!$status) { print ' node-unpublished'; } ?&gt;"&gt;

  &lt;h4&gt;&lt;?php print t('Job Title'); ?&gt;&lt;/h4&gt;
  &lt;p&gt;&lt;?php print $job_node-&gt;title; ?&gt;&lt;/p&gt;
  &lt;h4&gt;&lt;?php print t('Vacancy Number'); ?&gt;&lt;/h4&gt;
  &lt;p&gt;&lt;?php print $job_node-&gt;field_number[0]['value']; ?&gt;&lt;/p&gt;
  &lt;h4&gt;&lt;?php print t('Closing Date'); ?&gt;&lt;/h4&gt;
  &lt;p&gt;&lt;?php print $closing_date; ?&gt;&lt;/p&gt;
  &lt;h4&gt;&lt;?php print t('Job Summary'); ?&gt;&lt;/h4&gt;
  &lt;p&gt;&lt;?php print $job_node-&gt;teaser; ?&gt;&lt;/p&gt;


Hope this helps