Customizing your comment submitted by information in Drupal

Wow, that's a long title...
So I was playing around with the comment form on this blog today and figured I'd write down what I did to customize the way the submitted by information is output in case any one else would like to know, or if I forget and need something to look back at in six months.

So here goes.

Originally when some one would leave a comment on the blog, their comment would be displayed with their name and date info something like this:

"Submitted by Anonymous on March 11, 2011 - 11:45am."

Well now if that isn't ugly and boring I don't know what is.
First I decided to attack the Submitted by text. I decided I just wanted to post their name. So I did a little digging on drupal.org and found a theme override I could use here. Basically all I did was copy the code you see on that api page and pasted it into my template.php file replacing the word "theme" with the name of my actual theme.

<?php
function theme_comment_submitted($comment) {
  return
t('Submitted by !username on @datetime.',
    array(
   
'!username' => theme('username', $comment),
   
'@datetime' => format_date($comment->timestamp),
  ));
}
?>

Now you don't need to know a lot of code to do this. Look at what you just pasted and you should be able to deduce where the text is that you'd like to change. I took out everything except for the name and date. Upload the updated template.php file to your server, clear your cache and walla! At least overriding stuff always makes me feel cool :)

Now about that date field. I didn't want it to be in the format it was in. Which, by the way, seemed to default to my "medium" date format that you can configure at administer -> Site Configuration -> Date and Time. I needed more flexiblity than that would provide me though because I don't necessarily want to display the time the comment was posted.

So I did some more digging and found my answer on api.drupal.org yet again. You can see that our date is being created by something called date_format(). Looking at the api for this we can see all the parameters we can put in there. Pardon my terminology if I'm not calling certain things by their correct name. Basically we can specify which date format we want, or we can do a custom date format by doing something like this:

format_date($comment->timestamp, 'custom', 'F j, Y')

So the final bit of code in my template.php file looked like this:

function defaulttheme_comment_submitted($comment) {
  return t('!username : @datetime',
    array(
    '!username' => theme('username', $comment),
    '@datetime' => format_date($comment->timestamp, 'custom', 'F j, Y'),
  ));
}

and that got me exactly what I was after. The only drawback I see about this is that it will do this for all comment forms on your content types, so the page comments will be like the blog comments. I wanted consistency on my site so that was fine with me but if you need to format your comment form differently based on different content types, well I'm not sure how you'd do that, I'm still learning too!

Hope this helps some one out there!