Add Honeypot to Simplenews subscription form block

The Drupal Honeypot module (https://drupal.org/project/honeypot) works well out of the box for comments and web forms in nodes.

In my case i needed to add the Honeypot spam filter to a Simplenews (https://drupal.org/project/simplenews) newsletter subscription form, which resides inside a block in the sidebar of most pages.

The problem here was that if the time restriction feature is active, the Honeypot module disables the cache on pages where it is used (which is on most pages). I could have disabled this feature all together, but it is just another layer of protection that i didn’t want to loose.

So the way to get around this is to target a specific form and add or alter the Honeypot feature specifically. This is done by creating a custom module.

Step by step

Create a folder in the module section (sites/all/modules), and give it the name of your module. Example: overwrite_simplenews

Add two files to the folder.

  1. overwrite_simplenews.info
  2. overwrite_simplenews.module

The .info file should contain something like this (change as required):

name = Overwrite Simplenews
description = Customization
package = Mail
core = 7.x

The .module file should contain something like this (change as required):

/* Add Honeypot to simplenews subscription form */

function kleinermann_form_alter(&$form, &$form_state, $form_id){
	if ($form_id == 'REPLACE_WITH_FORM_ID') {
		honeypot_add_form_protection($form, $form_state, array('honeypot'));
	}
}

You need to replace REPLACE_WITH_FORM_ID with the ID of the form you want to target. To find the form ID, you can check your page source in the browser with Firebug, or if the theme doesn’t output this information, you can use the Devel module. After activation of the Devel module, put this inside your module to output the form ID:

function kleinermann_form_alter(&$form, &$form_state, $form_id){
   dsm ($form);
   dsm ($form_id);
}

For more details check out this great video by Peter Yaworski: https://www.torontowebsitedeveloper.com/drupal-video-tutorials/drupal-6-simplenews-subscription-block-override-tutorial-1-2

If you check the form now in the source of your browser output you should find that there is an extra hidden field in your subscription form, named as specified in the Honeypot settings page.

Improve your Drupal theme markup with body classes

Since Drupal 6, a lot of the detailed markup as found in the excellent Zen theme framework has made its way into the default Drupal markup. More details about this feature can be found here https://drupal.org/node/163723

To use this feature for your theme just insert the variable

 
$body_classes 

into your tpl files body opening tag

 
 

like this

 

The result will be something like this

 

How to create and use a Drupal template (tpl) file

Template files are used to overwrite the core Drupal output (or the output of a module that overwrites the core Drupal output).

This can be for example useful if the layout of a particular content type, page, block or view can’t be easily fixed by a stylesheet. With a tpl file you can completely overwrite all output by using HTML/ CSS/ PHP/ Javascript/ …

This can be done by placing tpl.php files inside the theme folder. The naming of the files needs to follow a hierarchy in order for Drupal to pick up exactly which file you’d like to overwrite.

The hierarchy structure is as follows (top overwrites bottom):

For pages:
page-node-edit.tpl.php
page-node-1.tpl.php
page-node.tpl.php
page.tpl.php

For nodes:
node-type.tpl.php
node.tpl.php

For comments:
comment.tpl.php

For blocks:
block-module-delta.tpl.php
block-module.tpl.php
block-region.tpl.php
block.tpl.php

For boxes:
box.tpl.php

Taxonomy:
page-vocabularyname-termname1-termname2.tpl.php
page-vocabularyname-termname1.tpl.php
page-vocabularyname.tpl.php

Some popular examples:
page-front.tpl.php (home page)
page-admin.tpl.php
page-login.tpl.php
search-theme-form.tpl.php (search form)
search-result.tpl.php (search result)

Often a good start after you have created the new file is to copy and paste the content of the template you want to overwrite into your new template. Template files can be found in the following locations:

Drupal core themes: themes
Drupal core modules: modules
Modules: sites/all/modules
Themes: sites/all/themes

For more information see the Drupal.org website: https://drupal.org/theme-guide/6-7

x


Angebot anfordern

Ich melde mich innerhalb von 24 Stunden zurück.