Enabling Mollom protection for forms via an install profile or update hook

Part of my series of blog posts on Useful Drupal update hooks

Most Drupal modules these days integrate with Features / CTools exportables in some shape or form making it easy to deploy configuration changes easily or even completely re-build your site from scratch. However, there are a few modules such as Mollom that lag behind slightly and require you to write install and update hooks to initialise / deploy configuration. The Mollom module has a ticket open to provide exportables for mollom forms but until that work is complete we will need to use the Mollom module's helper functions in our install routines. (Side note - Mollom's global configuration is stored in normal Drupal variables so these can be exported to code via Strongarm or just stuck in settings.php if that's how you like to work)

Without further ado, here is some sample code to apply mollom protection to the comment form for the "foo" content type. The example uses a helper function called _foo_install_mollom() which does the actual work and this is then called from both hook_install() and hook_update_N() functions. If you are still in the development phase and are rebuilding your site regularly from scratch you could skip the update hook.

The above code should be placed in a file called "foo.install", alongisde "foo.info" and "foo.module" where "foo" is the name of your custom module. N.B. Install hooks can't be placed in a theme, they must be in a module. You can find a tutorial for creating Drupal modules here.

mollom_form_save() is essentially a wrapper around drupal_write_record() so it could also be used in update hooks (see hook_update_N() to deploy changes to mollom forms that are already protected. To do this, simply change the desired values in your mollom config and increment the number on the update hook. Running update.php on your live site will then deploy this change.

This pattern of using hook_install() and hook_update() is useful for other modules such as the WYSIWYG module which does not yet have Features integration in a stable release (Although as per this ticket - Exportables and Features support for WYSIWYG 7.x - Features support is present in wysiwyg-7.x-2.x-dev. The maintainers are unfortunately holding up a stable release due to other pending issues.)

Tags