Modifying the privatemsg module

yoeld - July 5, 2009 - 06:12

Hello,

I am using the privatemsg module for enabling sending private messaging between my site registered users.

By default, the module add a link "Send this user a message" to the user profile. But I would like to have a nice icon rather than a text link.

I have been able to do that by modifying slightly the function privatemsg_user of the privatemsg.module file:

function privatemsg_user($op, &$edit, &$account, $category = NULL) {
  global $user;

  switch ($op) {
    case 'view':
      if ($url = privatemsg_get_link(array($account))) {
$myimage='<img src="link to my picture.jpg" alt="My Alt Text" class="images">'; <== This is what I added
        $account->content['privatemsg_send_new_message'] = array(
          '#type'   => 'markup',
          '#value'  => l(t('Send this user a message'), $url, array('query' => drupal_get_destination())),
          '#weight' => 10,
  '#link' => l($myimage, $url, array('html' => drupal_get_destination())), <== This is what I added
        );
      }
      break;
    case 'login':
      if (variable_get('privatemsg_display_loginmessage', TRUE) && privatemsg_user_access()) {
        $count = privatemsg_unread_count();
        if ($count) {
          drupal_set_message(t('You have <a href="@messages">%unread</a>.', array('@messages' => url('messages'), '%unread' => format_plural($count, '1 unread message', '@count unread messages'))));
        }
      }
      break;
  }
}

Since I think it is not good to tweak itself the code (because of maintenance and upgrades), my question is what should I do to have it in a separate entity. Using the template.php file is not appropriate because it is not themeable.

Thanks for every possible hint.

Is it possible to use CSS to

slayerment - July 5, 2009 - 06:19

Is it possible to use CSS to hide the text and do the image as the background?

Good idea! I will check

yoeld - July 5, 2009 - 06:56

Good idea! I will check this.

Thanks.

...

Keyz - July 5, 2009 - 07:44

In case you're unable to focus in on the right spot with CSS (perhaps there's no specific enough class or other element) then my suggestion is to use String Overrides module. You can see in your code above that the text is wrapped inside of a t() function. This means Drupal can translate the text... and it also means that using String Overrides you can easily replace it with any other text you want. As you can see, HTML is also permitted inside so what you could do is copy the part of the t() function between the two single quotes '' ... paste that in String Overrides, and for the replacement, add a div with a class, or even just an image tag if you want.

In any case you should Never modify the module itself though... this will lead to tons of trouble in the future as upgrades need to be applied (especially security updates which you cannot skip).

I gave it a try but it

yoeld - July 6, 2009 - 05:59

I gave it a try but it overrides into a text and not to a html tag.

Any hint on how to do that?

Don't hack the module

rschwab - November 11, 2009 - 00:15

Rather than hacking the module and having problems when updates come out, I added this code to my tpl.php files where I wanted a "message this user" button:

<a href="/messages/new/<?php print($account->uid); ?>?destination=user%2F<?php print($account->uid); ?>"><img src="mybutton.jpg" border="0" /></a>

Works like a charm.

- Ryan

p.s. I guess this code is best used on the user profile page, otherwise drupal might not understand what you mean by $account->uid. On my tpl phps it follows a profile_load_profile($account).
p.p.s. And this is all assuming you have a completely custom user-profile.tpl.php... otherwise the send a message text will still be output as part of $content or $profile.

 
 

Drupal is a registered trademark of Dries Buytaert.