Hi,

in my project i needed a contact form for anonymous users,
displayed only on a specific node-type and the subject-field should be prefilled
with the node- title and the node id.

I get these working - but I am pretty sure that it could be done in a much more cleaner way
with drupal hooks/modules/api. Due to the fact that i am new to drupal, I would like to hear your opinions
how I can handle things like this in a better, more stable way, without hacking the core.
So here is what I have done:

At first, i patched the drupal contact-module, to allow anonymous users to use the contact form:
http://drupal.org/node/58224
http://drupal.org/files/issues/anonymous-contact-5.3.patch

After that, i enabled the authors-information-block and set it to display only on the specific node- type.
Than i modified the profile module to integrate the authors contact- form on the author-information-block,
by adding the following line:

if ($fields) {
          $profile = _profile_update_user_fields($fields, $account);
          $output .= theme('profile_block', $account, $profile, TRUE);
          // ADDED: display authors contact form beneath his profile information.
          $output .= '<div id="author-contact">'.drupal_get_form('contact_mail_user', $account).'</div>';
        }

Then, I have done some coding in the contact module:
In function contact_mail_user($recipient):

1. Check if we are on the specific node- type:

// Are we on the 'angebot' node type?
  $match = FALSE;
  $types = array('angebot' => 1);
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load(array('nid' => $nid));
    $type = $node->type;
    if (isset($types[$type])) {
      $match = TRUE;
    }
  }

2. If we are - do some prefilling of the subject and message field:

// if we are on the node type 'angebot', dont include the 'to'- field, prefill the subject and message fields:
  if ($match) {
//Subject comes with node id and node-title
    $form['subject'] = array('#type' => 'textfield',
      '#title' => t('Subject'),
      '#value' => t('Request to Service No. ').$node->nid.': '.$node->title,
      '#maxlength' => 70,
      '#required' => TRUE,
    );
//prefilled with static text    
     $form['message'] = array('#type' => 'textarea',
      '#title' => t('Message'),
      '#value' => t('I am interested in your service. Please contact me.'),
      '#rows' => 15,
      '#required' => TRUE,
    );
    
      // integrate the nodes authors (email receipient) id in the form, because we will need it later
     // when we will send the mail 
      $form['uid'] = array('#type' => 'hidden',
      '#value' => $recipient->uid,
  );
  
  }
 

And last but not least,
get the authors id (receipient) we have used in the hidden field above.

function contact_mail_user_submit($form_id, $form_values) some lines

  // This comes from the anonymous Contact Form on the Content Node, so catch the authors id
  if ($form_values['uid']) {
    $account = user_load(array('uid' => $form_values['uid'], 'status' => 1));
  } 

Done.

Comments

Zoologico’s picture

Too bad you have not gotten a reply.
I am trying to do the same thing, but cannot hack core to get it accomplished.
:(
Hope we see someone respond.

francewhoa’s picture

Here are 2 modules that can do something similar. They are for Drupal 5.x. Both work without hacking Drupal's core and anonymous users can use them.

  1. Author Contact
  2. Anonymous Contact

The module Author Contact better match my needs. It is display in a block so you can easily configure it to only show on the nodes or the specific pages you want. Plus it's compatible with Captcha module.

---

How to contribute to Drupal.

Loving back your Drupal community result in multiple benefits for you