Skip to main content

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

An article from ComputerMinds - Building with Drupal in the UK since 2005
17th Feb 2009

Mike Dixon

Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

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

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.