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(&$form, &$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, &$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' => $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->type != 'job') {
drupal_access_denied();
exit;
}
<div id="node-<?php print $node->nid; ?>" class="node<?php if (!$status) { print ' node-unpublished'; } ?>">
<h4><?php print t('Job Title'); ?></h4>
<p><?php print $job_node->title; ?></p>
<h4><?php print t('Vacancy Number'); ?></h4>
<p><?php print $job_node->field_number[0]['value']; ?></p>
<h4><?php print t('Closing Date'); ?></h4>
<p><?php print $closing_date; ?></p>
<h4><?php print t('Job Summary'); ?></h4>
<p><?php print $job_node->teaser; ?></p>
Hope this helps