How to display comments in different place?
pielgrzym - February 24, 2007 - 13:38
Hi peeps,
I've got a layout in which content of the CCK node is displayed in one column and comments are displayed in the other column parallelly to the content. That's the layout idea, but I can't achieve it. I made a custom node-content_my_article.tpl.php which prints only node title and node terms. But I still can post comments under the content. I didn't print $links but the damn comments (and 'comment or login' links too) keep appearing :((((
Anyone, pls help! :(((

Jesus...
Could someone at least tell me how to catch comments before they are glued to the end of content? It's far the dumbest idea to make the whole comments page be sticked to the end of content without the ability to print it wherever we want :(( aaargh. so frustrated :[
--
Pielgrzym
Patrially solved
Thanks to helpfull people from drupal-support irc channel we figoured out a way to move comments:
It's for Drupal 4.7.x:
<?php
function mytheme_comment($comment, $links = array()) {
our_comment($comment, $links);
return '';
}
function our_comment($comment = array(), $links = array()) {
static $output = '';
if (!empty($comment)) {
$output = '<div class="comment'. ($comment->status == COMMENT_NOT_PUBLISHED ? ' comment-unpublished' : '') .'">';
$output .= '<div class="subject">'. l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid") . ' ' . theme('mark', $comment->new) ."</div>\n";
$output .= '<div class="credit">'. t('by %a on %b', array('%a' => theme('username', $comment), '%b' => format_date($comment->timestamp))) ."</div>\n";
$output .= '<div class="body">'. $comment->comment .'</div>';
$output .= '<div class="links">'. theme('links', $links) .'</div>';
$output .= '</div>';
return $output;
}
else {
return $output;
}
}
?>
Then we simply invoke our_comment() wherever we want in out theme :)
It actually catches the data passed to theme function and prints it out somewhere in the theme using an exact function copy. The only problem is the damn comment form. The comment form still appears under the node. I tried overriding mythem_comment_form the same way, but didn't succeed.
I beg for some help ;)
--
Pielgrzym
I'm trying to get this to
I'm trying to get this to work on 5.1 but it isn't working correctly...
Will post my solution once I get it.
didn't work for drupal 5
Looking in to how to solve this.
CSS
Maybe the easiest way is to try and use CSS and position:absolute etc to seperate the main node from the comments rather than php.
i dont believe this this
i dont believe this
this messes up my complete template design
i need to place the comments into a certain div
there is always a variable for everything like $titles $messages $missions etc.
WHY not for COMMENTS
Here ya go. Simplified.
Here ya go. Simplified. Scrap your previous code and place inside template.php.
<?php
function phptemplate_comment_wrapper($content = '', $type = 'comments') {
// Cache values with these static variables.
// Will fill when $content has value and $type is 'comments' --default.
static $all_comments;
// Will fill when $content has value and $type is 'form'.
static $comment_form;
// Default output.
$output = '';
// if $content has a value then fill static variables depending on $type.
// else fill $output to return below.
if (!empty($content)) {
if ($type == 'comments') {
$all_comments = $content;
}
elseif ($type == 'form') {
$comment_form = $content;
}
}
else {
if (isset($all_comments)) {
$output .= $all_comments;
}
if (isset($comment_form)) {
$output .= $comment_form;
}
}
// Double check $output before returning the surrounding markup.
if (!empty($output)) {
return '<div id="comments">'. $output .'</div>';
}
}
?>
<?phpfunction phptemplate_comment_form($form) {
// returns nothing. Just intercept and let comment wrapper handle output.
theme('comment_wrapper', drupal_render($form), 'form');
}
?>
After that, all you need to call is
theme('comment_wrapper'). Assign it to a variable or print directly from node.tpl.php or page.tpl.php. Haven't tested. It should do though.Good luck.
edit:: Oh, and this is for Drupal 5. Don't think there's a wrapper function in 4.7 but you get the idea. You should be able to make up a similar function.
comment form is now shown twice
thank you very much
the "redirect" of the comments works
but the comment form is now shown twice
once below the nodecontent and once below the redirected comments
but only the submit-button of the first form works
any idea how to fix this ?
Yeah, I see. It's do to how
Yeah, I see. It's do to how the comment form is called up and unfortunately it runs through theme_box() which is a very general theme function. I tried messing with it but there's no way to get the context reliably. An alternative would be to go through the comment administration page and set the comment form to output under the comments instead of on a separate page.
After that, you can simplify the function down to this:
<?php
function phptemplate_comment_wrapper($content = '') {
// Cache values with these static variables.
static $all_comments;
// Default output.
$output = '';
// if $content has a value then fill static variable.
// else fill $output to return below.
if (!empty($content)) {
$all_comments = $content;
}
elseif (isset($all_comments)) {
$output .= $all_comments;
}
// Double check $output before returning the surrounding markup.
if (!empty($output)) {
return '<div id="comments">'. $output .'</div>';
}
}
?>
I don't think anything else can be done about it from the theming end. If it bothers anyone, your free to create an issue. Search first though. :)
thanks a lot for your help i
thanks a lot for your help
i created as you suggested a issue http://drupal.org/node/161139 but didnt get any respond yet
the last days i tried to catch with an adapted version of your code the comment forms and place them were i want them to be
<?phpfunction mytheme_box($title = '', $content = '', $region = 'main') {
// variable to save form
static $comment_form;
// Default output.
$output = '';
// try to catch the comment form by its titles or url
if($title == 'Post new comment' || $title == 'Reply' || $title == 'Edit comment' || (arg(0) == 'comment' && arg(1) == 'edit') || (arg(0) == 'comment' && arg(1) == 'reply') || $title == 'test'){
// save the content being send with drupals function and dont pass it
if (!empty($content)) {
$comment_form = $content;;
}
// with my call i dont send any content but pass the content from the previous call
else {
$output .= $comment_form;
}
}else{
$output = '<h2 class="title">'. $title .'</h2><div>'. $content .'</div>';
}
return $output;
}
?>
it actualy works fine but the forms and their previews are formated in a silly way
in my layout there are two areas
one for the page-view nodes with black background
and below a white one for the comments
the black area has a fixed size and contains text, images or videos and the white area contains their comments
i think its not realy a exotic way to display content
the following problem also appears when im not placing the comments and their forms somewere:
-in the page view of a node - the node and its comments are at the right places
-when i reply a note - they are also at their right places
-when i preview a node reply - the note content is now below the comment area
(the node is getting appended to the form in comment.module, function comment_form_add_preview() line:1623 with (
<?php$form['#suffix'] = node_view(node_load($edit['nid']));)
?>
)
i can remove this with hook_form_alter()
-when i reply a comment - the node content is gone and at its place the parent comment is shown
-when i preview a comment reply - the node-content is gone and the parent comment is shown below the comment form
-when i preview a comment edit - the node-content is gone
i can get the nodes back in page.tpl.php with:
<?phpif(arg(0)=='comment'&&arg(1)=='edit'&&is_numeric(arg(2))){
$nid = db_fetch_object(db_query('SELECT nid FROM {comments} WHERE cid = %d', arg(2)));
print theme('node', node_load($nid->nid),false,true);
}
elseif(arg(0)=='comment'&&arg(1)=='reply'&&is_numeric(arg(2))&&is_numeric(arg(3))){
print theme('node', node_load(arg(2)),false,true);
}
elseif($_POST['op']=='Preview comment'){
print theme('node', node_load(arg(2)), false, true);
}
print $content;
?>
but there are still some comment previews flying around in my theme
i realy dont get the point of gluing nodes and comments in this not flexible and nonuniform way together
of course i also tried around with the comment settings "Display on separate page" and "Display below post or comments"
if somebody nows if there is another way to customize comment-forms than changing the core comments.module
please tell me
Doesn't work for me?
I tried using these comment snippets (in this post above: http://drupal.org/node/122240#comment-252946 ) on my site, but with no results.
I followed the instructions above: I pasted those two snippets into my template.php and called the "theme('comment_wrapper')" inside my custom tpl.php file for the nodes which comments I'd like to theme/move around.
I checked out that $output is returned, but it's returned empty. If I add something to the $output it gets returned - so at least the right function is called from my node's tpl.php.
I must be missing something here? As obvious as that "something" might be, would you mind pointing it out? I've been stuck with this for quite a while now..
Thanks!
HOW TO move the comment form to your liking...
Hi,
I ended up paying someone for a solution to this problem. Here is the best way around this problem, to date. The solution has been tested on Drupal 5.1. Unfortunately it requires you to hack the core - just be sure to make a note to modify this again when you do an update to drupal core.
If you want to move the comment form to your liking, you need to call it (the comment form and comments) in the node-nodetype.tpl file... Here we go.
Open up node.module file and look for:
<?php
function node_show($node, $cid) {
$node->cid = $cid;
$output = node_view($node, FALSE, TRUE);
if (function_exists('comment_render') && $node->comment) {
$output .= comment_render($node, $cid);
}
// Update the history table, stating that this user viewed this node.
node_tag_new($node->nid);
return $output;
}
?>
And modify that too:
<?php
function node_show($node, $cid) {
$node->cid = $cid;
$output = node_view($node, FALSE, TRUE);
// if (function_exists('comment_render') && $node->comment) {
// $output .= comment_render($node, $cid);
// }
// Update the history table, stating that this user viewed this node.
node_tag_new($node->nid);
return $output;
}
?>
Then, you go to your node.tpl theme file and add this snippet where you want to call the comment form and comments:
<?phpif ($page == 1):
if (function_exists('comment_render') && $node->comment):
print comment_render($node, $node->cid);
endif;
endif;
?>
If you have several node types, and you want to only move the comment form and comments on let's say, node type blog, you would do this instead.
<?php
function node_show($node, $cid) {
$output = node_view($node, FALSE, TRUE);
// modified this to be able to call the comment form + comments where needed
if ($node->type != 'blog'): // we are going to call it seperately, in the node-blog.tpl file...
if (function_exists('comment_render') && $node->comment) {
$output .= comment_render($node, $cid);
}
endif;
// Update the history table, stating that this user viewed this node.
node_tag_new($node->nid);
return $output;
}
?>
Then in the node-blog.tpl file, we add the same snippet as above:
<?phpif ($page == 1):
if (function_exists('comment_render') && $node->comment):
print comment_render($node, $node->cid);
endif;
endif;
?>
Please reference this post on other posts where you have read about this problem, so the drupal community will find this solution easier...
Happy comment form and comments moving!
Thanks!
Thanks for that!
I generally try never to touch core, but when it seems we've tried every possible alternative it looks like the only option.
Thanks for the help :)
It is my pleasure. We should
It is my pleasure. We should ask the drupal 6 team to consider a solution for the comment form and comments, so we won't have to hack the core the next drupal version.
No need to hack core
Thanks to litwol on IRC for thinking of clearing out $node->comment.
Put this where you want it in your node-TYPE.tpl.php:
<?phpif (function_exists('comment_render') && $node->comment) {
print comment_render($node, $node->cid);
$node->comment = NULL;
}
?>
Michelle
--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.
hey really i love u for this solution!!!!!!!!!!!
really i am soooooo happy now after doing my job
really thx for u and may god bless u
regards
mohd nashaat
Drupal 6 has same problem
I'm still seeing exactly the same problem in Drupal 6. The code that spits out the comment form is embedded in the node.module rather than being left to the theme designer. I don't want my comments within my node's node.tpl.php, I want them displayed elsewhere in my template's page.tpl.php. Is there no solution for this?
I've tried using litwol and Michelle's code above which will only work if the comments are set to display below the post and even then will display it twice as the node is still rendering the comments as well. If only they had made the very simple function node_show themable in node.module. That way we could choose to override the display of contents and add them in the template where we want.
I've submitted a feature request in the Drupal 6 queue, lets see if there are any takers... otherwise its hacking the node.module time.
For Drupal 6, you can try
For Drupal 6, you can try this in template.php:
<?php
function phptemplate_preprocess_page(&$vars) {
$vars['comments'] = $vars['comment_form'] = '';
if (module_exists('comment') && isset($vars['node'])) {
$vars['comments'] = comment_render($vars['node']);
$vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
}
}
function phptemplate_preprocess_node(&$vars) {
$vars['node']->comment = 0;
}
?>
I loved the referenced variables. :) It makes this possible.
You must clear the theme registry for this to work.
Now you'll have $comments and $comment_form inside page.tpl.php which will output only when your focused on a node. Make sure you set the reply form to separate page first. It will break the form's js behaviors. It could be a simple fix.
Oh, and forget about the feature request for 6. It's too late. Would be nice to make it straight forward for 7.
⎋ joon park
Nice one dvessel, this is a
Nice one dvessel, this is a good solution for Drupal 6.
I had to hack core
This didn't work for me - the comments & submission form replicated instead of just moving. But the core hack worked like a charm. Thanks!
Is there a way to remove the
Is there a way to remove the comment addition form from this?
This method doesn't work on php4
Note, this method, while elegant on php > 4, fails completely on php <= 4, due to the fact that objects are not passed by reference in php <= 4.
Views pager is gone..
<?phpif (function_exists('comment_render') && $node->comment) {
print comment_render($node, $node->cid);
$node->comment = NULL;
}
?>
While this solution did what it's supposed to, it also made the Pager for the view (inside which the nodetype in question is displayed) dissapear!
Can you guys think of any workaround for that problem: dissapearing pager?
And I can say.. I spent hours on figuring out where did my pager go..
-jamov
But the comment form ... ?
Thank you, Michelle, for this great hint. It really works well and allows me to put all the comments into a block.
But I am afraid only the existing comments are rendered in the block. But the comment form is still remaining at the end of the node.
How can I achieve to get it into the block too?
I have the same problem. I
I have the same problem. I don't know how to remove comment form and leave only comments.
I found (dirty) solution how
I found (dirty) solution how to hide comments form when we add this code above.
In my case I can do this only via css.
.view-content-frontpage .node #comments .box {display: none;
}
Unfortunately the form code is still in source of our page but it don't appear for visitor.
Few minutes ago I saw Panles 2 screencast (http://blip.tv/file/600413) and I think maybe this way we can also do that but I didn't try this yet.
Does this really work?
Well I didn't try your solution yet, but I imagine that by hiding the comment form by css it'll be gone at all. How can I get it shown up in another place?
Panels2 might be a good solution to get the form into another column. But I am afraid it'll not work in my case because I want the form to show up in a block.
subscribe
subscribe
Possible solution in D6
I seem to have a working solution in Drupal 6. This isn't totally tested yet, but so far it is working nicely and i have a lot of faith in it.
To do it, open up your template.php and adapt the following to yours:
<?php
function phptemplate_preprocess_page($vars) {
if (isset($vars['node'])) {
$vars['comments'] = comment_render($vars['node']);
}
}
function phptemplate_preprocess_node($vars) {
unset($vars['node']->comment);
}
?>
This should make a
$commentsvariable available to the page.tpl.php. Using the_phptemplate_variables()function, you should be able to do much the same in D5. This is just the first step I am sure, and I plan to play with it more over the weekend.--
Al "Zarabadoo" Steffen
You're making the same thing
You're making the same thing that another user has done some comments above…
See: http://drupal.org/node/122240#comment-707961
duplicate comments
The comments are rendered where I put the $comment variable in page.tpl.php but they are also appearing as part of the $content variable.
Am I correct in assuming that the code below should take care of that? (It's not on my site, even after clearing the cache.)
function phptemplate_preprocess_node($vars) {unset($vars['node']->comment);
}
By unsetting the node variable, is the idea that the engine will not append comments to the node when creating the $content variable for page.tpl.php?
in reference to the node hack
My question specifically is if the code for D6 is supposed to replace the node.module hack described by pielgrzym or if you still need that hack. (I resorted to using the hack for now.)
that's for the post guys!...but how do I...
how do I remove the existing comment now? I can move the comments to were ever I want but the existed comment is still under the node. How do I get rid of that?
Thanks, - Justin
Same question here - In
Same question here - In Drupal 5, how do you move the comment form?
Moving Comment Form
mcneelycorp's post from 9/19/07 (http://drupal.org/node/122240#comment-272809) is clearly described and works as advertised in Drupal 5.7
The problem is their
The problem is their solution requires you to modify the core, which I thought is a no no in the world of drupal?
It is a no no. But in my
It is a no no. But in my case, no one had an answer and cared to create a solution. It seems now there is another option but others are reporting problems with the newest snippet. Back to square one...
===
Elvis McNeely
Drupal services: http://www.elvisblogs.org/drupal