0

For some reason I cannot find answer to relatively simple question. How to insert some data (for example the "title" filed value) into comment body upon the comment submit. Finally I'd like to have in comment body something like this:

  • Node title (with some CSS markup) -
  • Comment body -

Could you please advice the right way to do this?

0

Since you didn't say which version of Drupal are you using, I'll assume that you're talking about D7.

You need to use hook_comment_presave().

function YOUR_MODULE_comment_presave($comment) {
  // Do this only for new comments.
  if (!$comment->cid) {
    // This code will insert the subject as heading.
    $subject = '<h2>' . $comment->subject . '</h2>';
    $comment_text = $comment->comment_body[LANGUAGE_NONE][0]['value'];
    // Set the value.
    $comment->comment_body[LANGUAGE_NONE][0]['value'] = $subject . $comment_text;
  }
}

However, do bear in mind that you will need to handle somehow editing existing comments as well.

Hope this helps.

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.