Appending markup to privatemsg message body First, a little background info:

I'm running privatemsg 7.x-1.2 across several sites running on a single D7 codebase. I have all of the privatemsg tables in one database, which is accessed by all of the sites as to keep private messages synchronised between them.

To enhance that little setup, what I would like to do is manipulate the message body at the time of insertion/saving to append a "This message was sent via [site name]" line at the very end of each message. This is how I achieved it at first:

function global_privatemsg_message_presave_alter(&amp;$message) {
    $message-&gt;body .= "&lt;div class=\"sent-via\"&gt;(". t("Sent via !site_link", array('!site_link' =&gt; get_site_link())) .")&lt;/div&gt;";
}


That worked well except I overlooked the fact that hook_privatemsg_presave_alter takes place prior to the input filter being applied, so if the user sends their message as "Filtered HTML" or "Plain text", the 'sent-via'  gets thrown out.

So, what I'm looking for is a reliable way of appending markup to the message body, or even introducing a new template variable that I can populate accordingly, without it being mangled by input filters (the result of Drupal's check_markup, I suppose).

Preferably I want to do this entirely within the constraints of the Privatemsg module API, but I appreciate that might be asking for the impossible.

Doable?
(I haven't used privatemsg) ... but sure it's doable because of a new feature in D7:


  Instead of attachments, private messages are fieldable and any number
  and combination of fields can be added to them.


So what you do is code up a custom module that defines a custom "sent_by_sitename" field that is a text field_type. Then in your presave hook you would just do:

$message-&gt;sent_by_sitename = 'some_sitename_in_your_multisite_install';


Then you could alter your message display to show this new field with a custom formatter that adds the markup and leading "This message sent by " markup.

I dont have the code to do this offhand (its probably about 30 lines). Here is a similar request of adding custom fields to the default user object in drupal -- your request is different regarding privatemsg but you probably will still use some of the same functions: add-properties-to-an-entity-hook-schema-alter

So in short:


Define your custom field in your custom module, eg hook_field_info()
on module_enable() and module_disable() attach your field to the message entity, eg field_create_field() and field_create_instance()
alter your existing privatemsg API call to set this new field value instead of raw HTML.
alter your privatemsg message display to show your new field with its associated field_formatter to be at the bottom of the message display.