Skip to main content

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

17th Feb 2009

Mike Dixon

Senior Mind

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