Pages

Monday, August 20, 2012

Disable Personal Contact Form in Drupal 7 Contact module


In the comments of a past post discussing best Drupal modules, it was recommended not to use Webform for contact forms. So for a project I am currently working on I decided to just use the Drupal 7 contact module that comes with core Drupal.
I have used this is the past and it works just fine. My requirements were simple, "provide a site-wide contact form that authenticated users can use to contact the site admins". On the surface it seems simple enough.
Getting this set up was extremely simple, just turn the module on, do a bit of configuration, and you easily have a simple working site wide contact form. The only problem is now every authenticated user has a checkbox that allows them to enable a "Personal contact form". While this is certainly a useful feature on some sites, this would only confuse the users of the site I was building and is definitely not in the requirements.
So the search began... I figured there must be a permission or a setting that can hide this option from users. The site I am building does not even allow users to see other user's profiles, so this option would not even be usable. However, despite my searching attempts I could find no such option.

Solution to hiding the Personal Contact Form Checkbox

There are a few options to default the personal contact form checkbox to checked/unchecked, but no option to hide this from users from the administrative interface of Drupal. In the days where usability of web applications is extremely important, having this unusable option available to users was not acceptable for me so I found the following two solutions:
1. Add this code to your template.php file of your theme (http://drupal.org/node/1233150#comment-5704336):
function yourtheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    unset($form['contact']);
  }
}
function yourmodule_form_user_profile_form_alter(&$form, &$form_state) {
  unset ($form['contact']);
}
Here is what I find wrong with these approaches:
  • If I add the first code to my theme, and decide to try out a different theme or build a new one later, I need to make sure to always add this code back or all the users on the site will begin seeing that personal contact form checkbox.
  • If I add the second set of code to a custom module, I have to manage extra code for the simple purpose of hiding unnecessary functionality. This is the better of the two options for me because I have multiple custom modules that this functionality would easily fit into. However, this seems like a lot of hassle if you need to create a module for only this one purpose.
All in all, these solutions are incredibly easy. I already have multiple custom modules and a custom theme for this website so it is really an easy fix for me. However, if you are building a simple site, and are not comfortable with PHP (which is true for a lot of new Drupal users), you might be better in all honesty to stick with using the Webform module for your site-wide contact form. If you don't plan on allowing authenticated users to login to your site, or don't mind the Personal Contact Form checkbox for your authenticated users, then the Drupal Core Contact module might be a good fit.
What do you think? Let me know in the comments below.

No comments:

Post a Comment

Thanks for your comment.