backdrop-contrib / honeypot

Backdrop port of Drupal module. Uses both the honeypot and timestamp methods of deterring spam bots from completing forms on your site.
GNU General Public License v2.0
3 stars 1 forks source link

Would be nice to be able to hook into the module to add other form ids to protect #29

Closed robertgarrigos closed 4 years ago

robertgarrigos commented 4 years ago

As far as I can see, there is no way to add form ids, to protect them, other than the ones already in settings. It would be nice to be able to hook into the module other modules from contrib modules.

serundeputy commented 4 years ago

@robertgarrigos here is the hook:

  honeypot_add_form_protection(
    $form,
    $form_state,
    array('honeypot', 'time_restriction')
  );
robertgarrigos commented 4 years ago

oh thanks. missed it!

herbdool commented 4 years ago

Funnily I hadn't thought of this. Might have better to implement something like captcha does.

robertgarrigos commented 4 years ago

mmmm maybe my issue wasn't clear. I believe this is not what I need. This honeypot_add_form_protection() hook is to add other types of protection to forms, but I need to protect my module's form with honeypot. I see that honeypot gets a list of forms to protect from the settings page with the function honeypot_get_protected_forms(), but cannot see a way to alter this list to add other forms. Is it?

Right. I actually see now that there is TODO note on that honeypot_get_protected_forms(): @todo - Add in API call/hook to allow modules to add to this array.

robertgarrigos commented 4 years ago

Something like this would do it, put at the end of honeypot_form_alter():

foreach (module_implements('honeypot_form_protect') as $module) {
        $forms_to_protect = call_user_func($module . '_honeypot_form_protect');
        foreach ($forms_to_protect as $protect_form_id) {
            if ($form_id == $protect_form_id) {
                honeypot_add_form_protection($form, $form_state, array('honeypot', 'time_restriction'));
            }
        }
    }

Then, other modules could implement this hook:

function may_module_honeypot_form_protect()
{
    return array(
    'my_module_form',
  );
}
herbdool commented 4 years ago

Hi @robertgarrigos good idea! I took your idea and adapted slightly in this PR https://github.com/backdrop-contrib/honeypot/pull/30. Please give it a spin and let me know if it's ready.

robertgarrigos commented 4 years ago

Tested it and worked as expected :-) Thanks!