I need a sitewide contact form, but I don't want my users to have contact forms because I'm using the Privatemsg module. The core contact module does not allow the administrator to turn off personal contact form functionality. So, is there another module I can use? Am I missing anything? If not, what do I need to do in terms of theming to just get rid of the Contact tab on user profiles?

I tried writing my own module to do this. It works 100% except that I can't get CAPTCHAs (from the CAPTCHA module) to work on it. If anyone could help with that, I'd be just as happy. :)

Thanks in advance.

Comments

francort’s picture

I hope this can help :)

d5.info

; $Id: Exp $
name = Custom module
description = erases personal contact tab
core = 5.x
package = can not be more optional

d5.module

function d5_menu($may_cache) {
  $items = array();
  
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      global $user;
      $account = user_load(array('uid' => arg(1)));
      if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
        $items[] = array('path' => 'user/'. arg(1) .'/contact',
          'title' => t(''),
          'access' => false,
        );
      }
    }
  return $items;
}

I believe this little module can help as a start point. I have done it in a hurry, for sure it can be better.
Maybe someone else has a better solution for this...

icecreamyou’s picture

Will that get rid of the tab or just disallow users from seeing the contact page itself? (In other words, does Drupal do an access check before displaying tabs?)

And does it actually work to redefine a menu item? If so, how does Drupal know what order to process them in (i.e., it could read d5.module first, and contact.module after, and the access setting would change)?

francort’s picture

Will that get rid of the tab or just disallow users from seeing the contact page itself? (In other words, does Drupal do an access check before displaying tabs?)

->I think so. I've tested a little more in the function changing the conditional parameters (such as user_access('administer users') ) and it woks different.
You can set your own access permissions( hook_access ) for the module, and give them instead "false" that i gived to this module.

And does it actually work to redefine a menu item?

-> There should be a cross point for redefine a menu item. I'm not 100% sure if this is the correct way to do so, but it works for me.

If so, how does Drupal know what order to process them in (i.e., it could read d5.module first, and contact.module after, and the access setting would change)?

-> I believe if you enable it later Drupal will proccess it later.

I have made a mistake with the code. It shouldn't be working for whatever $may_cache but when $may_cache is false.

function d5_menu($may_cache) {
  $items = array();
  if(!$may_cache){//no may_cache 
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      global $user;
      $account = user_load(array('uid' => arg(1)));
      if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
        $items[] = array('path' => 'user/'. arg(1) .'/contact',
          'title' => t(''),
          'access' => user_access('administer users'),//some access. set another if you need to
        );
      }
    }
   }
  return $items;
}

Anyway , hook_menu is a bit weird :)

icecreamyou’s picture

Thanks... will test soon. Now I need to figure out how to best hide the Personal Contact Form on/off checkbox setting.

francort’s picture

same module, hook_form_alter

first:

function d5_form_alter($form_id, &$form) {
   print_r( $form_id );
   print_r( $form );
    
}

go to the page you want to change the form. The form_id and form will be displayed.
When you know the id and what you want to make dispear from the form, just alter the form

function d5_form_alter($form_id, &$form) {
   if( $form_id == "id_i_know" ){
      unsset($form['something']);
   } 
}

Maybe it would need more conditional programming...like if( !user_access('bla bla')) unsset($form['something']); or something like that.

icecreamyou’s picture

Just wanted to post back here to say it worked... thanks very much.

Here's what I ended up with for d5.module, minus the ?> at the end.

function d5_menu($may_cache) {
  $items = array();
  if(!$may_cache) {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      global $user;
      $account = user_load(array('uid' => arg(1)));
      if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
        $items[] = array('path' => 'user/'. arg(1) .'/contact',
          'title' => t(''),
          'access' => user_access('administer users'),
        );
      }
    }
   }
  return $items;
}
function d5_form_alter($form_id, &$form) {
   if($form_id == "user_edit" && !user_access('administer users')){
      unset($form['contact']);
   }
}
gauravkhambhala’s picture

Will this code work in 6.* version. If not then how i can turn it up for 6.x ?

icecreamyou’s picture

No it won't, see hook_menu if you want to upgrade it.

francort’s picture

In Drupal 6 is easier. You have there hook_menu_alter

mtraherne’s picture

I am finding this code very hit and miss. After I installed it it worked perfectly, then 24 hours later it had removed the contact tab (and permission) to the wrong set of users effectively allowing all the wrong people to access the personal contact form.

Any thoughts or suggestions?

Personally I'd completely remove the personal contact form from the contact module and put it into a second module in the core.

fonant’s picture

I'm using this (change "myd6module" to your own module name):

function myd6module_form_alter(&$form, $form_state, $form_id) {
  // hide personal contact form settings for non-admin users
  if ($form_id == 'user_profile_form' && !user_access('administer users')) {
    unset($form['contact']);
  }
}

function myd6module_menu_alter(&$items) {
  // remove personal contact form page & tab for non-admin users
  $items['user/%user/contact']['access callback'] = 'user_access';
  $items['user/%user/contact']['access arguments'] = array('administer users');
}

Hides the setting tickbox and the contact form, unless you are logged in with "administer users" permission.

http://www.fonant.com - Fonant Ltd - Quality websites

ron collins’s picture

you'll need an info file to get it going. mine is below. i called the module "Hide Contact Tab" which is different than above. You can change it if you like:


; $Id: contact_tab.info,v 1.23 2008/12/08 01:26:54 thierrygd Exp $
name = Hide Contact Tab
description = "Hides the contact tab on user pages"
dependencies[] = contact
package = User interface

; Information added by drupal.org packaging script on 2008-12-08
version = "6.x-1.0"
core = "6.x"
project = "contact_tab"
datestamp = "1228700177"

ordually’s picture

Thanks for this. It worked great for my site!

shv_rk’s picture

heeeeeey works!

brei9000’s picture

Works great! Thank you!!!!!

wei.2k’s picture

Hi Thanks for the information, just what i needed.

After reading this comment and abit of further reading on http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6
i realised i can hide the contact section without installing any module, but it is only specific to the specified theme.

Simply copy these code below into your theme's template file and clear your cache.
Also, This is for D6 just in case someone wonder.

function themename_theme() {
  return array(
    'user_profile_form' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

 function themename_user_profile_form($form) {
 	  unset($form['contact']);
	  $output .= drupal_render($form);
	  return $output;
  }


/*Remove contact menu*/
function themename_menu_local_task($link, $active = FALSE) {
  if ( strpos($link,'/contact') ) {
    return '';
  } else {
      return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
  }
}

francort’s picture

I like the approach.
The only thing I would change would be:

  if ( strpos($link,'/contact') ) {

for

  if ( strpos($link,'/contact') === FALSE ) {

because when one uses strpos and if the code won't be executed when strpos returns false(it doesn't find the string) or when strpos returns 0(the string is in the first position) and both cases are the opposite :)

cheers

wei.2k’s picture

Ah.. thank you for pointing that out :)

dc1258’s picture

Hi! Thanks for this I have been trying to find a way around this and have not been able to find a solution.

I copied your code into my template.php file and it still shows the contact tab in user profiles. What am I doing wrong!? Is this not the right file? Where specifically should I paste it?

Please help me out! I am using the basic Garland theme.

dc1258’s picture

I got it to work now. I think the issue was the change from if ( strpos($link,'/contact') ) { to if ( strpos($link,'/contact') === FALSE ) {. I did not put the false statement in and it's working fine.

Is there a way to do this so that administrators can still see the "contact" link? How else can or would an administrator contact a user?

francort’s picture

add some user access statement:

//...code
  if(!user_access('administer users')) unset($form['contact']);
//...code

and

//...code
  if ( strpos($link,'/contact') && !user_access('administer users')) {
    return '';
//...code

The mistake on the strpos, was:

strpos($link,'/contact') !== FALSE
chicagotech’s picture

Between the module solution to remove the personal contact form/tabs and the template.php solution, is one of these more or less burdensome to overall site performance?

john.kenney’s picture

this thread, especially patch at #6 might be of interest to some here: http://drupal.org/node/138574#comment-928203

i installed this module and it does the trick (for new users only).

MakeOnlineShop’s picture

Hi,

Is there any better way to remove users personal contact form option that they can see when they EDIT their account ?

Thanks to all.

amontero’s picture

Just stumbled upon https://drupal.org/project/contact_permissions, that does the trick for me.