Drupal webform trick submitting hidden values

Posted by: 
Dominique De Cooman

How to submit some hidden values along with a drupal webform? Our example. We have job node (nid 1) and we want to go to a webform to apply for the job. So we build a link on our node pointing to the webform node (nid 2).
In the node template type quickly a url: node , nid webform, nid job node

<?php
print l('Apply for this job''node/2/1');
?>

In a module you define a hook_form_alter(). (Hook form alters are used to change forms in drupal and naturaly they work on webforms too)
Maybe a little side trick here: We build the id of our webform dynmicaly. The first part is 'webform_client_form_' the second part is a variable who is also dynamicaly build because we have a multilingual site, the i18n_get_lang() api function gets us the current users language. In the end we have something like this 'webform_client_form_2' where 2 is the node id. You could do this all hardcoded too.
Now to save the node id of our job in a hidden webform field we do the following. We grab the path break it up and in the array on key 2 we find our node id. We use the default value of the hidden field to store our node id. Use checkplain to sanitize the input. Submit the form and you 'll find the value submitted onto your hidden field.

<?php
/**
 *  Implementation hook_form_alter()
 */  
function glue_form_alter(&$form$form_state$form_id) { 
  if (
$form_id == 'webform_client_form_'.variable_get('glue_link_to_webform_' i18n_get_lang(), '2')) {
    
array_unshift($form['#submit'], 'glue_webform_add_nid'); 
    
$bpath explode('/' ,$_GET['q']);
    if (
is_numeric($bpath[2])) {
      
$form['submitted']['vacature_node']['#default_value'] = 'node/'.check_plain($bpath[2]);
    } 
  }
}
?>

In a webform hidden fields have some variables available but you cant really format them properly.

You can define the variables in a settings form like this:

<?php
/**
 *  Implementation hook_menu()
 *  <a href="http://api.drupal.org/api/function/hook_menu/6
">http://api.drupal.org/api/function/hook_menu/6
</a> */ 
function glue_menu() {
  
$items = array();
  
$items['admin/settings/custom'] = array(
    
'title' => t('custom settings page'),
    
'page callback' => 'drupal_get_form',
    
'page arguments' => array('glue_settings_form'),
    
'access arguments' => array('administer custom settings'),
    
'type' => MENU_SUGGESTED_ITEM,
  );

  return 
$items;
}

/**
 * The settings form for the module
 */
function glue_settings_form() {
  
$form['glue_link_to_webform_nl'] = array(
    
'#type' => 'textfield',
    
'#title' => 'Sollicitatie node nl',
    
'#default_value' => variable_get('glue_link_to_webform_nl''117'),
  );   
  
$form['glue_link_to_webform_en'] = array(
    
'#type' => 'textfield',
    
'#title' => 'Sollicitatie node en',
    
'#default_value' => variable_get('glue_link_to_webform_en''122'),
  ); 
  
$form['glue_link_to_webform_fr'] = array(
    
'#type' => 'textfield',
    
'#title' => 'Sollicitatie node fr',
    
'#default_value' => variable_get('glue_link_to_webform_fr''123'),
  ); 
  
$form['glue_link_to_webform_de'] = array(
    
'#type' => 'textfield',
    
'#title' => 'Sollicitatie node de',
    
'#default_value' => variable_get('glue_link_to_webform_de''124'),
  ); 
  
  
  return 
system_settings_form($form); 
}
?>

Comments

Drupal webform trick submitting hidden values

Add new comment

Who are you?


By knowing who you are you will be directed to a page where we have displayed all our drupal services relevant to your professional identity.