1

I am calling privatemsg_new in php in a block so that the form to contact the node author is on the same page as the content. here is the code.

$node = menu_get_object();
if ($node->uid != 0) {
module_load_include('pages.inc','privatemsg');
$recipients = $node->uid;
print "<div>Contact " . $node->name ." about this post.</div>";
$subject = $node->title ." question.";
print drupal_render(drupal_get_form('privatemsg_new', $recipients, $subject));

Normally you access this form from the node page via a link that reads 'send this author a message' and that link loads a 'destination' parameter into the url query so that, when the message is finished, it redirects you back to the node page.

My code for the form in the block works fine except for the redirection back to the page after submission. The variable that needs to be set is inside the scope of the privatemsg_new function:

http://drupalcontrib.org/api/drupal/contributions!privatemsg!privatemsg.pages.inc/function/privatemsg_new/7

How can I set that variable so that redirection works correctly?

EDIT 8/1/2012 Eugene's answer below was the correct direction and code for this particular problem, but I wanted to update my question with the exact code necessary to achieve a fully functioning 'Contact Author' block. Simply copy and paste this in for Drupal 7 and it should work great, if you looking for the same solution.

$node = menu_get_object();
if ($node->uid != 0) {
module_load_include('pages.inc','privatemsg');
$recipients = $node->uid;
print "<div>Contact " . $node->name ." about this post.</div>";
$subject = $node->title ." question.";
$form = drupal_get_form('privatemsg_new', $recipients, $subject);
// Setup destination parameter to be equal to current page.
$form['#action'] = url('messages/new/'. $node->uid, array ('query' =>    drupal_get_destination()));
  print drupal_render($form);
drupal_set_title($node->title);
}

You have to set the drupal title manually. The reason being that the privatemsg_new function overrides this.

2
+50

Try this code in block, if you don't want to implement any hook in custom module.

$node = menu_get_object();
if ($node->uid != 0) {
  module_load_include('pages.inc','privatemsg');
  $recipients = $node->uid;
  print "<div>Contact " . $node->name ." about this post.</div>";
  $subject = $node->title ." question.";
  $form = drupal_get_form('privatemsg_new', $recipients, $subject)
  // Setup destination parameter to be equal to current page.
  $form['#action'] = url('node/'. $node->uid, array ('query' => drupal_get_destination()));
  print drupal_render($form);
}
0

I would try implementing hook_form_alter, and adding a submit handler at the end of the submission handler chain to add a redirect. Eg,

function mymodule_privatemsg_new_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'mymodule_submit_adds_redirect';
}

function mymodule_submit_adds_redirect($form, &$form_state) {
  $form_state['redirect'] = '/your/destination/final/redirect/url';
}
1
  • mmm I would prefer not to write a module for this. The above code works great as php in a normal block. Anyway to do it within the block code itself?
    – blue928
    Jul 27 '12 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.