I'm trying to make a module for Drupal 7 that replicates the functionality that this custom module provided in D6: http://spinspire.com/article/limited-supply-options-lists-webforms.

The overall goal is to take a webform select options component and modify the available option list based on what's already been 'reserved'. For example, let's say I'm giving away three different laptops for free, and anyone can come and claim each laptop by submitting my webform. When one user claims "Laptop A" (submits a webform with that option), I don't want it to appear as an option in the select list anymore, because it's reserved. Essentially, each option in the select list may only be submitted once.

I took the original code and modified it so it works with D7, up to a point. When I dsm() the option array I see that the previous option has properly been removed. But it still shows up in the form. What gives?

function webform_limited_list_form_alter(&$form, $form_state, $form_id) {

//...
//omitted code to set the option that works just fine
//...
  
  if (!isset($form['#node'])) {
	return;
  }
  $node = $form['#node'];
  if(!empty($node) && $node->type == 'webform') {
	foreach($node->webform['components'] as $component) {
		if(isset($component['extra']['limited_supply'])) {
			_webform_limited_list_remove_used_options($node->nid, $component['cid'], $component['form_key'], &$form);
		}
	}
  }
}

function _webform_limited_list_remove_used_options($nid, $cid, $cid_name, &$form) { 
  $query = "SELECT data FROM {webform_submitted_data} WHERE nid = :nid AND cid = :cid";
  $args = array(
  	':nid' => $nid,
	':cid' => $cid,
  );
  $result = db_query($query,$args);
  foreach($result as $row) {
    $value = $row->data;
	$options = $form['submitted'][$cid_name]['#options'];
	unset($options[$row->data]); //here's where I unset the option
	dsm($options); //this call shows that the option has been removed, but I still see it in the select list
  }
}

Comments

Bagz’s picture

Yes, the option will have disappeared out of the $options array but you haven't modified $form. You need to update $form in your function. You are also reloading the $options variable in your loop so it would only remove the last one.

Try this instead:

$options = $form['submitted'][$cid_name]['#options'];
foreach($result as $row) {
    $value = $row->data;
    unset($options[$row->data]);
}
$form['submitted'][$cid_name]['#options'] = $options;
AvalancheOfLlamas’s picture

Of course! Thanks so much, that worked perfectly.

nunodacunha’s picture

Could you guys help?
I'm a bad programmer, i need help using the drupal 7 webform for our students choose the room for an event, lets say i need them to choose the maximum 4 beds of the room. If it´s asking too much just let them choose once the room.
So the can only choose example: Rom (M-001) once!
I tryed onther modules, but they have calendars other things...
Sorry bother, but i really need it!
Thank you

Amir Simantov’s picture

Used it in another context - helped me a lot - thanks!