phpdrupalhoneypot

How do I write a custom module in Drupal 8 to connect Honeypot to Simpleform?


I'm working on a custom module in Drupal 8 that should protect a Simpleform form with the help of the Honeypot module.

So far, I can enable my module in Drupal which works flawlessly, but the Honeypot protection isn't added to the form. One of my problems right now is that I don't know what the form ID is. That's why I'm trying to log the whole form - which also isn't working.

It seems that the whole code in my .module file is bypassed by Drupal when I load the page with the Simpleform in it. What am I doing wrong here?

My files:

overwrite_simplenews.info.yml

name: Overwrite Simplenews
type: module
description: 'Enable Honeypot for Simplenews plugin'
package: Custom
core: 8.x

overwrite_simplenews.module

<?php

function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
    if ($form_id == 'simplenews_subscriptions_block_dc6f06ce_ad69-46de_8d8d'){
            honeypot_add_form_protection($form, $form_state, array('honeypot'));
    }

    $message = 'Form content: <pre>' . print_r($form, TRUE) . '</pre>';
    \Drupal::logger('overwrite_simplenews')->notice($message);
}

Solution

  • All hooks have to be "renamed" according to your module name. For example, to use the hook_form_alter, you have to call your function: overwrite_simplenews_form_alter();