Score:0

How to disable the option time_restriction that honeypot add to the forms

cn flag

In the README file for the Honeypot module, it says:

If you want to add honeypot to your own forms, or to any form through your own

module's hook_form_alter's, you can simply place the following function call

inside your form builder function (or inside a hook_form_alter):

> \Drupal::service('honeypot')->addFormProtection(   $form,  
> $form_state,   ['honeypot', 'time_restriction'] );

Note that you can enable or disable either the honeypot field, or the time

restriction on the form by including or not including the option in the array.

I added this code to the buildForm function in the form I want to change, and also tried it with a hook_form_FORM_ID_alter().

  \Drupal::service('honeypot')->addFormProtection(
    $form,
    $form_state,
    ['honeypot']
  );

However, the $form['honeypot_time'] element still appears in the form. I only managed to remove it by adding unset($form['honeypot_time']) in my hook_form_FORM_ID_alter. I would like to use the way that Honeypot describes in its README file.

id flag
Did you configure honeypot to be active on all forms on the site?
cn flag
Yes, it's active on all forms.
id flag
I'm just guessing that the defaults are already in effect for your form because honeypot is already enabled on it.
Score:1
cn flag

The problem with the module honeypot is that they don't explain in the README file that this code only works when you don't enable for all the forms in the site.

honeypot_add_form_protection(
  $form,
  $form_state,
  ['honeypot', 'time_restriction']
);

To disable time_restriction you must use a hook_fomr_FORM_ID_alter() and unset the element inise the hook:

  if( (\Drupal::service('module_handler')->moduleExists('honeypot')) &&
      (isset($form['honeypot_time']))
     ) {
    unset($form['honeypot_time']);
  }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.