Community Documentation

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

Last updated May 5, 2010. Created by YK85 on May 5, 2010.
Log in to edit this page.

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'

Comments

could you make a brief

could you make a brief explanation about how to add this code to our privatemsg module please?. making a new module is hard for me right now :)

I needed slight tweak with 6.x-2.x-dev

I had to change all instances of $form['privatemsg']['recipient'] to just $form['recipient'] to get it to work.
And work well it does, cheers.

Drupal 7

Is the process the same in Drupal 7?

The code above works as is on

The code above works as is on Drupal 7 so long as you change all instances of $form['privatemsg']['recipient'] to $form['recipient'] as noted in the comment above.

Another solution that works for me with Drupal 7

<?php
function MYMODULE_form_privatemsg_new_alter(&$form, &$form_state, $form_id) {
 
// Check if a default value is configured for the to field.
 
if (!empty($form['recipient']['#default_value'])) {
   
// Convert the recipient field to a value type and force the default value.
   
$form['recipient']= array(
     
'#type' => 'textfield',
     
'#value' => $form['recipient']['#default_value'],
     
'#title' => 'Send message to',
     
'#disabled'=> TRUE,
     
'#weight' => -10,
    );
  }
}
?>

Use Prepopulate

if you use the prepopulate module you don't even need to write code.
Just link to the form and pass the usernames in the URL:
messages/new?edit[recipient]=test13,test14

This will prefill the usernames but they can still be edited.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Themers

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.