Disable Personal Contact Form in Drupal 7 Contact module

By shane
Fri, 2012-05-04 03:47

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') {

2. Build a custom module and add this code (http://stackoverflow.com/questions/2540582/how-do-i-disable-the-personal...):

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.

Update from the comments

As mentioned in the comments below, It is probably best to use:

$form['contact']['#access'] = FALSE;

instead of:

unset ($form['contact']);

in your theme or module.

Need to learn more Drupal?

Premium Drupal Training Online (Horizontal)


It's best not to use unset($form['anything']). Rather, use $form['anything']['#access'] = FALSE.

Thanks for the good advice. I agree that I prefer to prevent access than completely remove it from the $form array. Just out of curiosity, do you have any reasons as to why it is actually a better method?

Thanks again, I will update my post to include this recommendation.

Or just don't give the permission. "Use users' personal contact forms " to the roles that shouldn't have access or see it.

While it hides the users message tab on other users profile pages if I set that up, it does not hide the checkbox in the users edit page. As I mentioned above, that means the users will see a checkbox in their edit page that has no use whatsoever throughout the site. The users of my site can not even view other users profiles (they do no have access to that permission), yet they can see this checkbox in their account settings. This is a big usability issue.

I am not sure if this should be considered a bug or possibly a feature request. It looks like there may be a contrib module that attempts to fix this problem (recommended in a comment below).


Perhaps we can get this rolled into the contact module in a future release.

I've been going through the same thinking and steps on various sites I've built. It should really be an option in the 'contact' module.

I'm also curious about unset() vs '#access' -- what is the difference in behaviour?

What about using Panels, and making the user edit form by yourself letting the checkbox for the personal contact out of the form.

I have not tried this in panels, but it is a good recommendation. Have you tested this? I know in panels there are limitations to formatting the node edit form, but I am not sure on the user edit form.

Hm, it seems that Panels is not the right way.
I can only choose the hole user-edit-form or user-view-form. There is no way to select the part "personal contact form", so there is no way of excluding it.
Maybe someone else has made it through Panels, or you need another module to split the Form in peaces and than select the proper parts in Panels.

That is the way that panels handles the Node edit form as well. The entire form must be output, there is no selective options to limit which parts of the form are displayed. I think the best option at this point is to either use the Contact Permissions module (listed in a different comment), or use the form_alter function as mentioned in the actual blog post.

Thanks for testing out the Panels option.

Thanks for the module, will check it out. I really think this should be built directly into the contact module as it is a very useful feature as far a usability is concerned, but this is a great start.

Post new comment