Hello

This is a great module. I'd like to know if it is possible to parse arguments to private message. Maybe it's better to just explain the situation:
- I will include a link "contact author" on several diferent nodes.
- The idea would be to include the node where the person clicked to generate the page.
- In the message, for example the subject would be: [nid] subject here or something. Or even something in the message itself where I could identify the node.

thanks !
Leo

Comments

berdir’s picture

Status: Active » Postponed (maintainer needs more info)

I am not sure if I exactly understand what you mean.

There are several ways to act or hook in when a message is sent (hook_form_alter, our own hooks, see http://drupal.org/node/369399). If you know enough about PHP, this shouldn't be a real problem. You could either display default values with hook_form_alter or append/prepend/replace something directly in the message before it's getting saved. The only thing you need to figure out is how to get your data you want to insert.

What we could provide is a easy way to set the subject/body to default values based on GET Parameters.

Feel free to ask specific questions about the implementation if you have any.

mcfly-1’s picture

Thanks for your answer. Let me expand on my example.

Suppose my website has 100 books for sale. A customer might want to contact me regarding a specific book, so I might have a link "Contact store regarding this book". What happens frequently is that the user forgets to specify what he/she is referring to, or might be too general. Privatemessage would insert a subject or even a snippet in the message body indicating where that click originated "[book: Shakespeare] i have a question !"...

Does that make sense ?

thanks
Leo

berdir’s picture

Title: parsing arguments to private message » Allow to set subject and body with query arguments
Category: support » feature

The easiest way to do this is something like the following:

1. Add a link to privatemsg which somehow contains the nid you want to refer to. For example, you can set use messages/new/your_uid?destination=/node/123. If you do this, the user will also be redirected back to your node when the private message is sent.

2. Either create a new module mymodule or put the following code inside one of your own modules (rename mymodule to the name of your module)


function mymodule_form_privatemsg_list_alter(&$form, &$form_state) {  
  // check if there is a destination argument
  if (isset($_REQUEST['destination'])) {
    // check if the first part is node and the second a node id that can be loaded with node_load()
    if ((arg(0, $_REQUEST['destination']) == 'node') && ($node = node_load(arg(1, $_REQUEST['destination']))) 
        // set default value to [node-type: node-title]
       $form['privatemsg']['subject']['#default_value'] = t('[@type: @name]', array('@type' => $node->type, '@name' => $node->title));
    }
  }
}

The code is provided without warranty and is untested :)

We may include a easier way to set the subject in the future, but you still need to load the node so that you know the type and name.

I am changing the issue a bit to make it a feature request for adding a subject variable..

Kakulash’s picture

So currently the only thing that can be passed in the URL is "destination"?

Tested the solution above and it broke the site :(

Will check if syntax and everything was correct...

nathaniel’s picture

Thanks this can be very useful.

<?php
/**
 * Pass RE:[node title] as privatemsg subject field.
 */

function mymodule_form_privatemsg_new_alter(&$form, &$form_state) { 
  // check if there is a destination argument
  if (isset($_REQUEST['destination'])) {
    // check if the first part is node and the second a node id that can be loaded with node_load()
    if ((arg(0, $_REQUEST['destination']) == 'node') && ($node = node_load(arg(1, $_REQUEST['destination'])))) {
        // set default value to [node-type: node-title]
       $form['privatemsg']['subject']['#default_value'] = t('RE:[@type - !name]', array('@type' => $node->type, '!name' => $node->title));
    }
  }
}
?>
mcfly-1’s picture

Thank you so much. I will try this and see what happens !

best
Leo

mcfly-1’s picture

Sorry, this does not work for me. The redirection portion works, however I can't have the subject changed at all. Any ideas ?

thanks
Leo

nathaniel’s picture

What code are you using for the link?

It has to look something like this:

"http://yoursite.com/messages/new/" . $node->uid . "?destination=node/" . $node->nid
mcfly-1’s picture

Still no luck. This is what I am using:

<a href="/messages/new/<?php print $node->uid ?>?destination=node/<?php print $node->nid ?>">Contact author regarding this</a>

thanks !
Leo

berdir’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

#398916: Remove hook_form_alter in favor of a more flexible privatemsg_new form will allow to set the subject with a GET argument, setting this to duplicate.