There are a number of changes I would like to make to the personal contact form, but I don't know how. Can anyone point me in the right direction?

Thank you.

Comments

Drave Robber’s picture

D6:

Contact form is built by: http://api.drupal.org/api/drupal/modules--contact--contact.pages.inc/fun...
You can alter it using: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo... (or hook_form_alter(), not much difference in most cases)
Example:

function modulename_form_contact_mail_user_alter(&$form, &$form_state) {
  // We're not going anywhere. (by default, submitting a contact form redirects to user profile)
  $form['#redirect'] = FALSE;
  // Replace 'Subject' textfield with a choice of two subjects.
  $form['subject'] = array(
    '#type' => 'radios', 
    '#title' => t('Subject'), 
    '#options' => array(
      // Values are displayed to user submitting the form, but keys are used in composing e-mail – 
      // that's why we need them identical.
      t('I could use some help') => t('I could use some help'),
      t('I don\'t need help, just wanted to say...') => t('I don\'t need help, just wanted to say...'),
    ),
    '#required' => TRUE,
  );
  // Make message area 5 rows instead of 15.
  $form['message']['#rows'] = 5;
}

Note that you can also leave the default user contact form intact and alter a copy (mapped by hook_forms()) instead.

jg314’s picture

@Drave, sorry I should have mentioned that I'm working in Drupal 7. Do you know where I can find the form there?

Thanks for the help.

dianacastillo’s picture

Diana Castillo

jg314’s picture

Awesome @Drave, thanks so much for pointing me in the right direction. Now I can't seem to get the hook to work correctly.

I am calling the following code:

<?php
function custommodule_form_contact_personal_form_alter(&$form, &$form, $form_id){
  unset($form['to']);
  dvm($form);
}
?>

When I look at the $form array through the dvm call I don't see any of the form elements there and the to element still appears. When I tried to use hook_form_alter instead of hook_form_FORM_ID_alter I got the same result.

What am I missing here?

Drave Robber’s picture

Function arguments?

hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)

You have &$form twice there.

jg314’s picture

@Drave, haha, wow. Thanks. Talk about glancing over a glaring mistake.

I appreciate the help throughout.

jg314’s picture

Since it makes sense that those who edit the form would also want to edit the email that is sent out I figured I would post my code for that as well. Here is the implementation of hook_mail_alter I used for the drupal 7 user contact form.

<?php
/**
 * Implements hook_mail_alter(&$message).
 */
function YOURMODULENAME_mail_alter(&$message) {
  if ($message['id'] == 'contact_user_mail') {
    $language = $message['language'];
    $variables = array(
        '!site-name' => variable_get('site_name', 'Drupal'),
        '!site-mail' => variable_get('site_mail', ''),
        '!subject' => $message['params']['subject'],
        '!sender-name' => format_username($message['params']['sender']),
        '!sender-url' => $message['params']['sender']->mail,
        '!recipient' => $message['params']['recipient']->field_first_name['und']['0']['value'] . ' ' . $message['params']['recipient']->field_last_name['und']['0']['value'],
    );
    
    $message['subject'] = t('!site-name - !subject', $variables, array('langcode' => $language->language));
    unset($message['body']);
    $message['body'][] = t('Hi !recipient,', $variables, array('langcode' => $language->language));
    $message['body'][] = t("!sender-name (!sender-url) has sent you a message from the !site-name website.  The message is:", $variables, array('langcode' => $language->language));
    $message['body'][] = $message['params']['message'];
    $message['body'][] = t("To respond to their email just reply to this message.  If you don't want to receive e-mails, you can email us at !site-mail.  Thanks for being a part of the community.", $variables, array('langcode' => $language->language));
  }
}
?>
dianacastillo’s picture

I found it easier to modify it in template.php as described here http://bri-space.com/content/theming-contact-form-drupal-7-contactsiteform

Diana Castillo

thewrush’s picture

By default the personal contact form is setting the title using the API Contact Personal Form:

drupal_set_title(t('Contact @username', array('@username' => format_username($recipient))), PASS_THROUGH);

How would I go about changing the title to use the Real Name of the person if we are using the module Real Name?

I'm guessing I will need to create a custom module and use a hook? I don't know how or where to begin doing this.

jaypan’s picture

Create a module, implement hook_form_alter() and use drupal_set_title() to set the title you want.

Contact me to contract me for D7 -> D10/11 migrations.