Author image
Senior Mind

Altering drupal comments in hook_comment op = validate (drupal 5)

One of those little Drupal annoyances this, there is now easy way to alter a comment (using hook_comment) before the comment is written to the DB. Hook comment is invoked during the validation phase, with $op set to "validate", and the comment array is passed by reference - but unfortunately any changes made to the comment are not passed on to the comment save function. A quick and slightly dirty way around this is to make use of the global $form_values array and manipulate the comment directly in there - this little example will set an empty comment title to "(No subject)" rather than allowing the comment to default to using the first X characters of the comment itself.

function yourmodule_comment(&$a1, $op){
  if ($op=='validate'){
    global $form_values;
    if (trim($a1['subject']) == '') {
      $form_values['subject'] = t('(No subject)');
    }   
  }
}

Comments

Hi People
How are you doing?

Hi,

Thanks for that really useful, I'm a Drupal developer and managed to implement this nicely.

Thanks,
Ed

Comments on this article are now closed, if you want to give us feeback you can use our contact form instead.