In Axaj comments in Drupal 5: How I made it – Part I I covered the most important part of the module code: saving comments to the database. Now, let’s see how to display them properly.
Rendering the new comment
Comments usually have more information than the user name and the comment text. In my website, for instance, comments have information like the user registered date, where he/she lives, the country flag, the comment number on the thread, etc. All this information could not be made up at the client side; the server had to send it back to the client once the comment was saved.
Also, we want to take full advantage of Drupal: we want the corresponding filters applied to the comment text to remove malicious HTML tags, convert plain URLs into HTML links, etc.
The best way to do this is to use the function theme with the parameter ‘comment_view’. The difference between theme(‘comment’,…) and theme(‘comment_view’,…) is that filters are applied only in the second case, if I am not mistaken.
-
function _spaniards_get_latest_comments($nid, $fromcid) {
-
$result = db_query('select * from comments where nid = %d and cid > %d', $nid, $fromcid);
-
$content = '';
-
while ($comment = db_fetch_object($result)) {
-
if ($comment->status == COMMENT_NOT_PUBLISHED) continue;
-
$links = module_invoke_all('link', 'comment', $comment, 1);
-
$content .= theme('comment_view', $comment, $links);
-
}
-
return $content;
-
}
As I mentioned before, we don’t want to return just the comment added by the user but any comment added by any other user since the user loaded the page. This function will return any comment on the thread with comment Id greater than the parameter $fromcid.
Besides using theme(‘comment_view’,…) it is a good idea also to invoke the link hook. This will let other modules like fasttoggle inject their very useful links in out comment.
And that is it for now. This code, together with the code on Part I will be the only PHP needed. We will see the client side Javascript code on Part III.
Series:
Ajax comments in Drupal 5: How I made it – Part I
Ajax comments in Drupal 5: How I made it – Part II
Ajax comments in Drupal 5: How I made it – Part III
See the ajax comments in action
Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is www.spaniards.es
Related Posts:
Ajax comments in Drupal 5: How I made it – Part II
In Axaj comments in Drupal 5: How I made it – Part I I covered the most important part of the module code: saving comments to the database. Now, let’s see how to display them properly.
Rendering the new comment
Comments usually have more information than the user name and the comment text. In my website, for instance, comments have information like the user registered date, where he/she lives, the country flag, the comment number on the thread, etc. All this information could not be made up at the client side; the server had to send it back to the client once the comment was saved.
Also, we want to take full advantage of Drupal: we want the corresponding filters applied to the comment text to remove malicious HTML tags, convert plain URLs into HTML links, etc.
The best way to do this is to use the function theme with the parameter ‘comment_view’. The difference between theme(‘comment’,…) and theme(‘comment_view’,…) is that filters are applied only in the second case, if I am not mistaken.
As I mentioned before, we don’t want to return just the comment added by the user but any comment added by any other user since the user loaded the page. This function will return any comment on the thread with comment Id greater than the parameter $fromcid.
Besides using theme(‘comment_view’,…) it is a good idea also to invoke the link hook. This will let other modules like fasttoggle inject their very useful links in out comment.
And that is it for now. This code, together with the code on Part I will be the only PHP needed. We will see the client side Javascript code on Part III.
Series:
Ajax comments in Drupal 5: How I made it – Part I
Ajax comments in Drupal 5: How I made it – Part II
Ajax comments in Drupal 5: How I made it – Part III
See the ajax comments in action
Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is www.spaniards.es
Related Posts: