3

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:

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

Here's hoping some drupal guru can come to my rescue! thanks in advance!

1
  • would I have to create a custom token?
    – maGz
    Commented Jul 24, 2011 at 23:04

2 Answers 2

3

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

11
  • wiifm, u are a saviour!!! please gimme a few minutes to read over this again and understand it before I attempt implementing it. Thank you for taking the time to assist me
    – maGz
    Commented Jul 25, 2011 at 0:44
  • I'm guessing that site_profile gets replaced with my theme name, right?
    – maGz
    Commented Jul 25, 2011 at 0:50
  • site_profile in the above example was the name of a custom module
    – wiifm
    Commented Jul 25, 2011 at 2:01
  • Ah, I see. If you don't mind, I've more or less understood everything, but just wish to inquire about 2 things: (1) could you please explain the $form_state variable a bit more; and (2) I'm guessing that $job_node->field_number[0]['value'] writes that value to the first webform field?
    – maGz
    Commented Jul 25, 2011 at 2:27
  • sorry, I don't have enough rep to vote your answer up, but I'm certain this is the correct one. Please could you just provide a little bit further info based on my previous comment.
    – maGz
    Commented Jul 25, 2011 at 2:28
1

Here is a short howto I wrote on getting custom code to run with Webform. It details writing a custom module, copying the Webform template to match the nodeID, and adding custom javascript/jquery for form processing.

http://docs.quantact.com/drupal-webform-custom-php-code

1
  • Thznks Tim, that's very helpful
    – maGz
    Commented Sep 24, 2011 at 8:11

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.