HowTo: Make 'To: [username]' show as Non-Editable Text when UIDs are in URL

Last updated on
30 April 2025

Credit: Thanks to Berdir for contributing the code!

The below code will replace the 'To: [input box]' with static text 'To: username' when there are uid values in the URL on the New Message page, but keep the 'To: [input box]' when there are no uid values in the URL (aka no recipients are not indicated yet)

I use the Views integration to populate a list of users and a link to Send Message. When you click the Send Message it takes you to the New Message page with the username already populated. I didn't want to show the input box with the username populated but rather non-editable text that says 'To: username'

It is as easy as adding the below code into a custom module (http://drupal.org/node/231276), flush cache and try it out.

<?php
function yourmodule_form_privatemsg_new_alter(&$form, &$form_state) {
  // Check if a default value is configured for the to field.
  if (!empty($form['privatemsg']['recipient']['#default_value'])) {
 
    // Display a visible markup element.
    $form['privatemsg']['recipient_display'] = array(
      '#value' => t('To: @recipients', array('@recipients' => $form['privatemsg']['recipient']['#default_value'])),
      '#weight' => -10,
    );

    // Convert the recipient field to a value type and force the default value.
    $form['privatemsg']['recipient']['#type'] = 'value';
    $form['privatemsg']['recipient']['#value'] = $form['privatemsg']['recipient']['#default_value'];
  }
}
?>

Note:
- this works for multiple recipients as well www.mydrupalsite.com/messages/new/125,126 will show 'To: username1, username2'

Help improve this page

Page status: Not set

You can: