Contact forms is great, thanks for creating the module. I've been using it a lot to extended over the short-comings of Drupal's default contact form.

I had a project where I wanted my readers to be able to contact users on the site, but I wanted to pre-fill a subject in those emails to direct the conversion properly.

I created the module, but then noticed that there's an issue where anonymous users can not use the User Contact forms, so I had to include some code from this thread. http://drupal.org/node/58224#comment-1155557 as well.

Here are the features of my module.

Two features of the module aside from pulling in the patched code.

user/1/contact/Subject of the email
This will send an email to UID=1 with the pre-filled subject of "Subject of the email"

user/1/contact/123
This will send an email to UID=1 with the pre-filled subject of "Re: [node:nid=123]->name"

If node (nid=123) has the title of "My Node Title" the contact form will be pre-filled with "Re: My Node Title". I also check that the user who is emailing about a node has permission to view that node (un-tested though)

user/1/contact
** My module does not override this menu item, so it will use the default code in contact.module in which anonymous users can not email users. You will have to switch the priority of contact_users.module menu item (if you can do that in D6) or switch the path of the menu item to allow for this.

The code could be cleaned and some extra features added, but I wanted to make you aware that this code exists in case you wanted to include it into future versions of contact forms. If you want me to improve upon it before inclusion, please let me know what features you want and all implement them when I have time.

If you're not interested incorporating this into contact_forms module, we can create another module for this...but I think it's best combined into contact_forms as it provides and uses much of the same code.

Oh, PS. I noticed you had some ?????? in your hook_menu. You can find the documentation on loaders here: http://drupal.org/node/209056

CommentFileSizeAuthor
contact_users.tar_.gz3.66 KBj0rd

Comments

behindthepage’s picture

Hi Jordan,

Glad you have found Contact Forms useful. At the moment we have a patch for Drupal 7 that will add all of Contact Forms functions into core so soon there will be no more Contact Forms.

I had a quick look at your module and think it all can be replaced with the following code once you have patched core to allow anonymous users to use the personal contact forms. A very interesting thing about Drupal is that if it can't find the path for "user/1/contact/Subject of the email" it will go to "user/1/contact" then you can use arg(3) in hook_form_alter to do everything you have done without duplicating all the code from the contact module.

This is very rough code but I think it does the trick.

<?php

/**
 * Implementation of hook_form_alter()
 */
function contact_users_form_alter(&$form, $form_state, $form_id) {

  // Alter all user contact forms
  if ($form_id == 'contact_mail_user') {

    if (arg(3) != ''){

      $subject = contact_users_subject_load(arg(3));

      $form['subject'] = array(
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#maxlength' => 50,
        '#required' => TRUE,
        '#default_value' => $subject,
        );
      }
    }
}


function contact_users_subject_load($subject) {
   global $user;
   if(is_numeric($subject)) {
      // pull subject from nid
      // it's an NID, we get the subject from the node instead
      $contact_node = node_load(array('nid' => $subject));
      if(!node_access('view', $contact_node, $user)) {
         // user can't view node, we don't want them to be able to send emails about it
         drupal_set_message(t('You are unable to view this node '), 'warning');
         return FALSE;
      }
      $subject = t('Re: !subject', array('!subject' => $contact_node->title));
   }
   else {
        $subject = str_replace('_', ' ', $subject);
   }
   return $subject;
}
?>

It looks like a permission to allow anonymous users to send email via the personal contact form will be added to core very soon so that is part of your problem solved and I think being able to set the subject via the url should be in core too. How about posting a patch? Check out our discussion on the contact forms -> core patch here http://drupal.org/node/183678 Then, if you wanted, you can publish a module that does the node title thing.

Regards
Geoff

behindthepage’s picture

Status: Needs work » Closed (fixed)