I can confirm that the privatechoice works fine with webform module using hook_form_alter() - eg:

/**
 * Implementation of hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_NID') {
    $form['submitted']['COMPONENT']['#privatechoice'] = TRUE;
  }
}

Remember to change the form_id, and a component name in the code above.
Awesome contribution BTW, It would be nice to see a webform-privatechoice.module for non coders.
I'll give it some thought, because if it's as easy as dropping the module in and enabling it I could see privatechoice being quite popular.

CommentFileSizeAuthor
#2 privatechoice_webform.tgz881 bytesChristopher Herberte
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

donquixote’s picture

Thanks for the feedback.

I was hoping that the maintainers of webform (that is, quicksketch) would accept the patch at
#282436-50: Multiple Options, Same Value in 'Select' Form Element (+ Protect from Spam Harvesters)
or alternatively, develop a built-in solution that does the same, copying privatechoice.

No reactions yet.

I am sure it would be possible to extend privatechoice with some dedicated form_alter code to change webform from the outside, but I think it would be much easier to have the component-specific logic in webform directly.
What should we do?

Christopher Herberte’s picture

FileSize
881 bytes

I would not rely on webform maintainers to accept the functionality, instead prove the feature's popularity first. Obviously the inclusion of features into webform which are not going to be used is pointless.

I've written a module to do exactly as described above with webform. It adds a checkbox to the component. needs testing but works for me, and i'm using this on a production site as of next week so i hope it does not break.

One issue I found is that private choice does not work with webform checkboxes and radios -- not a big deal for me.

Christopher Herberte’s picture

Instructions for the above module:

Note: this only works for webform Select "List Box" component at present.

* Extract and enable module as per standard installation procedures, see http://drupal.org/node/70151 for further information.

* Create new or edit existing webform, go to Webform -> Form Components.

* Add or edit existing "Select options" component. In the "Edit component" form there is now a checkbox for "Enable Private Choice?", tick it, Submit.

* Check HTML source to check the option keys are in fact hidden.

donquixote’s picture

Looks like a good start.
Should we really make this a new (sub)module, or rather have it enabled by default? Atm I see a higher chance of someone forgetting to enable the privatechoice_webform submodule, than the chance of someone who does not want this extra feature added to webform.

It certainly does improve modularity, though. And it will allow privatechoice_webform to be replaced with something else: A solution built-in with webform, or another module which leverages privatechoice. If admin_menu had that modularity, then dqx_adminmenu would contain less copy-paste.

So, with your approval, I shall include this code as a submodule. And maybe I can make it work for checkboxen and radios, too.

Christopher Herberte’s picture

Awesome, go for it. I'm looking forward to seeing privatechoice_webform in a future release.

donquixote’s picture

Storage: Would we gain anything from storing this stuff in the webform_component table, instead of a custom variable?

The only real benefit I can imagine at this moment is if there are large numbers of webform nodes, then we can avoid loading their settings all at once. But that is not very likely.

Note: Serialized array in the 'extra' column.

Christopher Herberte’s picture

extra column would be a better place for it. i see also webform_component_insert hook in webform.components.inc - if that's the right place to start. will take a look later

donquixote’s picture

Just sent a contact form message to quicksketch.
Would be nice, if we get some feedback.

And yeah, there might be some hidden treasure in the webform api!

dooug’s picture

webform 6.x-3.11
privatechoice 6.x-dev

The private choice wasn't working for me when I set the #privatechoice to TRUE. I investigated a bit, and saw the process & element_validate functions weren't being added to the element, so I did it in my hook_form_alter (below). I believe those two lines shouldn't be necessary for me, but it was the only way I could get it to work.

For some reason the privatechoice module's process & element validate functions were only being added/called on the date & time elements on my webform (and not my other select option elements). I tried playing with the module weights in the system table but that didn't seem to do it.

/**
 * Implementation of hook_form_alter().
 */
function MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_9') {
    $form['submitted']['number_of_attendees']['#privatechoice'] = TRUE;
    // these two lines shouldn't be necessary?, but here they are
    $form['submitted']['number_of_attendees']['#process'][] = 'privatechoice_process';
    $form['submitted']['number_of_attendees']['#element_validate'][] = 'privatechoice_validate';
  }
}

donquixote’s picture

What is the element type of the $form['submitted']['number_of_attendees'] ?

The #process and #element_validate should be set in function privatechoice_elements() - can you check what is going wrong there?

dooug’s picture

The #process and #element_validate should be set in function privatechoice_elements() - can you check what is going wrong there?

It seems privatechoice_elements() worked, but only altered the #process and #element_validate for my date/time select options, but not the other select option elements.

the form element #type is radios

donquixote’s picture

Strange.
Could you check if some other module is overwriting the #process and #element_validate?
Not sure where you would have to look..
Or maybe that's a matter of module weight?

dooug’s picture

Yeah, I tried to figure out if any other modules were altering the form or if the modules' weight were causing them to fire out of order. It is likely some other module is causing this, I just thought I'd share my temporary solution. Hopefully, I'll find time to figure it out soon.

donquixote’s picture

You could visit
yoursite.com/devel/elements
this will give you some useful information :)
(if you have devel enabled)

dooug’s picture

thanks for the suggestion, it looks like the privatechoice #process & #element_validate functions are there for radios

Now that I think about it, I suspect my issue is caused by my webform module being a bit out of date. It looks like this like in webform/components/select.inc resets those functions (instead of appending them into the array). Although, I am perplexed because the webform module weight is -1, so it shouldn't happen after private choice.
$element['#process'] = array('expand_radios', 'webform_expand_select_ids');

donquixote’s picture

The modifications from hook_elements(), which privatechoice uses, are applied before any hook_form_alter().

dooug’s picture

I updated to webform 6.x-3.14.

It seems line 366 in webform/components/select.inc sets the #process functions for the radios element. If I comment out that line, privatechoice works (but those process functions from webform obviously don't work). I haven't figure out what prevents all of these process functions (from webform and privatechoice) from being called.

----UPDATE: -----
I made a tweak to webform/components/select.inc. It appears the webform renders the element but doesn't use the #process functions that have already been set by any hook_elements. I am unsure my patch is the correct way to do this....

diff --git a/sites/all/modules/webform/components/select.inc b/sites/all/modules/webform/co
index 8f57f94..3ba6ed6 100644
--- a/sites/all/modules/webform/components/select.inc
+++ b/sites/all/modules/webform/components/select.inc
@@ -363,7 +363,9 @@ function _webform_render_select($component, $value = NULL, $filter = TR
     else {
       // Set display as a radio set.
       $element['#type'] = 'radios';
-      $element['#process'] = array('expand_radios', 'webform_expand_select_ids');
+      $all_elements = module_invoke_all('elements');
+      $element['#process'] = $all_elements['radios']['#process'];
+      $element['#process'][] ='webform_expand_select_ids';
     }
   }

I found this issue was already pointed out in the Webform issue queue: http://drupal.org/node/1188774#comment-5219542
So, the problem is in webform not privatechoice. Thanks for the help, hopefully the webform module maintainer fixes this issue soon.