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)');
    }    
  }
}


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

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

as far as i can tell, if i do a browser refresh of a form - hit the Reload button in the browser, and resend the form submission - $form_state['storage'] is erased.

Have your say ...

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br><pre>
  • Images can be added to this post.
  • Lines and paragraphs break automatically.