Enabling CAPTCHA support on webforms is more error-prone than it ought to be - several of the documented methods don't seem to work anymore due to possibly API changes. The following will enable support for all CAPTCHAs, might it be worth adding to the Webform module?
/**
* Implementation of hook_captcha_default_points_alter().
*
* Enable CAPTCHA integration for all Webforms.
*/
function webform_captcha_default_points_alter(&$items) {
// Get the list of supported Webform content types.
$webform_types = webform_node_types();
if (!empty($webform_types)) {
// Get a list of all nid's for all supported webform content types.
$webforms = array();
$nodes = db_select('node')
->fields('node', array('nid'))
->condition('type', $webform_types)
->execute();
foreach ($nodes as $node) {
// Webform form IDs follow this format.
$webforms[] = 'webform_client_form_' . $node->nid;
}
// Enable CAPTCHA integration for each form, if it isn't already enabled.
foreach ($webforms as $form_id) {
if (!isset($items[$form_id])) {
$captcha = new stdClass;
$captcha->disabled = FALSE;
$captcha->api_version = 1;
$captcha->form_id = $form_id;
$captcha->module = '';
// This may be 'node', 'default', 'captcha/Math',
// 'recaptcha/reCAPTCHA', etc.
$captcha->captcha_type = 'default';
$items[$form_id] = $captcha;
}
}
}
}
Comments
Comment #2
damienmckennaIn patch form.
Comment #3
danchadwick commentedI agree that the feature is desirable. When integration code is needed between two modules, I look to the installed base of the two modules. Are there more webform users that don't use captcha or vice versa? The installed base of captcha is about half that of webform. Therefore it would be better if this code were put into captcha.
Closing this issue for the context of the webform issue queue, but you might move it to the captcha issue queue and re-open it.
Comment #4
damienmckennaIn that case I'll move it there.
Comment #5
ddrozdik commentedThis patch is outdated. Needs re-roll.
Comment #6
naveenvalechaComment #7
Pradnya Pingat commentedThis patch applied cleanly in webform module because the changes is in webform module. so removing reroll tag.
Comment #8
acbramley commentedNow that CaptchaPoints are config entities based on the form ID this won't work any more. There's also the case where the webform is referenced in a webform field from a node, those form IDs then contain the node id of the referencing node so it isn't viable to do an alter in this case.
I think a better solution would be for the Captcha module to provide a WebformElement plugin so users can simply add the element to any form they like. I might have a look at implementing this tomorrow.
Comment #9
borisson_This sounds like a solid solution, has anyone tried pinging @jrockowitz to validate this approach?
Comment #10
jax commentedIf webform would always provide a base_form_id
webform_submission_webform_machine_namethen this problem would be automatically fixed, I think.I'll see if I can get this working, my D8 fu is limited.
Comment #11
jax commentedIf you addwebform_submission_formas captcha point /admin/config/people/captcha/captcha-points the captcha will appear on all webforms.That doesn't seem to work. Sorry.
Comment #12
jrockowitz commentedI think this should be integrated into the Webform for 8 as a third party setting. (similar to how Honeypot is setup).
The challenge/issue is that I don't think CAPTCHA is working great on multi-step forms.
Comment #13
jzavrl commentedI don't think we can fix the issue like this. Instead, I found a different issue #2839891: Allow to target the base_form_id which provides you with the option to use the base form ID. So you can only put in one webform ID and it will work for all.
Comment #14
Mike Dodd commentedany one have any luck with getting this to work in D8
Comment #15
jzavrl commented@Mike Dodd, have a look at #13.
Comment #16
acbramley commentedFunnily enough, I just discovered today that Webform does already provide an element for Captcha integration this issue should probably be closed as anyone can add these elements to any webform already?
Comment #17
jzavrl commentedYes, you can. There is an element available in the builder. Although the functionality is limited to what Webform as a module is providing as far as I know. Not sure if you can use that to add in reCAPTCHA for example.
Comment #18
acbramley commented@jzavrl you can :) The element lets you choose the challenge type. The only issue I've found with it so far is #2886323: Captcha element does not hide for users with the skip CAPTCHA permission
Comment #19
jzavrl commentedAh I see. Well in that case we have a workaround to provide CAPTCHA for each Webform separately, an issue which provides us with a base form ID and another issue which in not related to this directly. I think we can close this issue mainly because of #2839891: Allow to target the base_form_id.