Last updated 26 August 2016. Created on 11 February 2008.
Edited by rhuffstedtler, jdwfly, chellman. Log in to edit this page.

Let's say you have a few users that you want to be emailable through your contact form (and you don't want to use webform module). That's great, just set up a category for each one. But if you just link to the contact page, your visitors will have to manually select the person they want to email after they hit it. There's a quick way around that.

Drupal 5

Create a module (or use an existing one if you have one for glue code like this) that contains this function:

// change mymodule to the name of your module, of course
function mymodule_form_alter ($form_id, &$form) {
  // changes the default value in the contact form if we have a ?c=x in the URL
  if ($form_id == 'contact_mail_page') {
    if ((int)$_GET['c'] > 0) {
      $form['cid']['#default_value'] = (int)$_GET['c'];
    }
  }
}

Now you can use links in this form:

<a href="contact?c=2">Email the second person in the contact form</a>
<a href="contact?c=4">Email the fourth person in the contact form</a>

Until personal contact forms can be used by anonymous users, this solution might stave off the temptation to patch core.

Drupal 6

The code needs to be changed in order to work for Drupal 6. Replace the word hook with the name of your module.

function hook_form_alter(&$form, &$form_state, $form_id) {
  // changes the default value in the contact form if we have a ?c=x in the URL
  if ($form_id == 'contact_mail_page') {
    if ((int)$_GET['c'] > 0) {
      $form['cid']['#default_value'] = (int)$_GET['c'];
    }
  }
}

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

patkoscsaba’s picture

The above described method didn't worked for me (maybe my mistake somewhere), so I ended up editing the contact modules code. I did the following:
- edit file contact.pages.inc
- locate function contact_mail_page
- at line 59 in that file, you should have: if (count($categories) > 1) {...
followed by description and an if for setting default category if none specified.
- I added the following at line 60 (inside the above mentioned if and before the default category for none specified command):

//If we have a default category defined in the URL
        if ((int)$_GET['c'] > 0) {
          $default_category = (int)$_GET['c'];
        }

- access is like in the original article

This will preselect the desired value. If nothing is specified as default value it will continue as before, and if default value is specified it should apply only to URLs not containing "?c=x"

chellman’s picture

I just tested the Drupal 6 code against Drupal 6.12, and it works just fine. Patching core like this, especially when there's an easily workable alternate solution, is a very bad idea.

Gold’s picture

I just contributed this;

http://drupal.org/project/anonymous_contact

It may be what people are after.

--
Regards
Gold
Drupal Code Monkey

zapo’s picture

We have a newsletter form that will just show the Email field. Works fine if you're logged in but if you try in an Incognito window you see a message that says an "Error occurred".