I'm using webforms, and would like to pass a value into a webform options fields via the url.

This works find with text fields, but for the options element, there doesn't appear to be a way to set a default value for the select using tokens (%get[foo] or [current-page:query:foo] depending on if you have the token module installed).

Also, would it be possible for this to work with manual lists as well as pre-built option list?

Can we have an extra field added to the options element to allow us to set the initial state of the options list by a token?

Something along the lines of http://drupal.org/node/296453#webform-url-default

Comments

jmoyles’s picture

For anyone stumbling across this request, here is a simplified version quick and dirty workaround I made using a hook function. This allows you to set webform values from the query string on the first load of the webform.

/**
 * Simplified implementation of hook_form_alter().
 */
function webform_hook_form_alter(&$form, &$form_state, $form_id) {
		// If first pass, set defaults
		if(empty($form_state['input'])) {
			webform_hook_load_defaults($form);
		}
	}
}

/**
 * Reads in the query string, and matches up passed parameters to webform fields
 */
function webform_hook_load_defaults(&$form)
{
	// Pull URL query parameters
	$queryparameters = drupal_get_query_parameters();
	
	// For each query parameter, try to match up against a default that needs to be set
	foreach($queryparameters as $key=>$value) {
		if(!empty($value)) {
			// Key has a value
			switch (strtolower($key)) {
				case 'foo': // Multi value checkbox
					$form['submitted']['foo']['#default_value'] = explode(",", $value);
					break;
				case 'bar: // Single value select
					$form['submitted']['bar']['#default_value'] = $value;
					break;
			}
		}
	}
}
quicksketch’s picture

This is already possible if you select "Manual entry" then paste in the token. The token will be maintained in all recent versions of Options Element/Webform, per #1007532: Setting default select option with %get[]. However I think this is a good feature request to think about generally, though I'm not sure that the solution will actually be in Options Element (it might be more suitable to live in Webform or the implementing module).

jmoyles’s picture

Good to know quicksketch. Does this also work with pre-built option lists?

quicksketch’s picture

Yep, pre-built select lists may have a default value set even though the options themselves may not be changed.

jmoyles’s picture

Status: Active » Closed (works as designed)

Most Excellent! Thanks quicksketch