如何在 drupal 中打印单个评论?

发布于 2024-07-15 17:28:49 字数 72 浏览 7 评论 0原文

我想根据评论 ID 在 drupal 中打印单个评论。 我怎样才能做到这一点? 谷歌和其他来源没有给我带来任何结果。 谢谢。

I want to print a individual comment in drupal based on it's comment ID. How can I do this? Google and other sources have yielded me nothing. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-07-22 17:28:49

如果您需要像 core 那样显示评论,包括来自节点的信息,伊顿的建议很好(除了它是 {comments},而不是 {comment})。 除了 modules/comment/comment.tpl.php 中的默认 theme_comment 实现不使用 $node。

但是,我的做法略有不同,因为如果您需要提取单个评论,则使用 comment.tpl.php 提供的正常内容格式显示它可能是不合适的。

function print_comment($cid) {
  $sql = "SELECT * FROM {comment} c WHERE c.cid = %d";
  if ($comment = db_fetch_object(db_rewrite_sql(db_query($sql, $cid), 'c'))) {
    return theme('my_special_comment_formatting', $comment);
  }
}

当然,受 comment.tpl.php 的启发,在模块的 hook_theme() 实现中定义这种特殊的注释格式。

2014 年 2 月更新:请注意,这是 2009 年的问题/答案。 在 Drupal 8 中,您只是不想访问假设的底层 SQL 数据库(并且无论如何也不会这样做,而是使用 DBTNG),而只是使用类似以下内容的内容:

if ($comment = entity_load('comment', $cid)) {
  return entity_view($comment, $view_mode);
}

Eaton's suggestion is good (except it's {comments}, not {comment}) if you need to display the comment like core does it, including the info coming from the node. Except the default theme_comment implementation in modules/comment/comment.tpl.php makes no use of $node.

However, I'd do it slightly differently, because if you need to extract a single comment, displaying it with the normal content formatting provided by comment.tpl.php is likely to be inappropriate.

function print_comment($cid) {
  $sql = "SELECT * FROM {comment} c WHERE c.cid = %d";
  if ($comment = db_fetch_object(db_rewrite_sql(db_query($sql, $cid), 'c'))) {
    return theme('my_special_comment_formatting', $comment);
  }
}

And of course, define this special commment formatting in your module's hook_theme() implementation, inspired by what comment.tpl.php does.

2014-02 UPDATE: note that this is a 2009 question/answer. In Drupal 8, you just don't want to access the hypothetical underlying SQL database (and would not do it like this anyway, but use DBTNG), but just use something like:

if ($comment = entity_load('comment', $cid)) {
  return entity_view($comment, $view_mode);
}
‘画卷フ 2024-07-22 17:28:49
function print_comment($cid) {
  $sql = "SELECT * FROM {comments} WHERE cid = %d";
  if ($comment = db_fetch_object(db_query($sql, $cid))) {
    $node = node_load($comment->nid);
    return theme('comment', $comment, $node);
  }
}
function print_comment($cid) {
  $sql = "SELECT * FROM {comments} WHERE cid = %d";
  if ($comment = db_fetch_object(db_query($sql, $cid))) {
    $node = node_load($comment->nid);
    return theme('comment', $comment, $node);
  }
}
残花月 2024-07-22 17:28:49

没有理由使用任何 sql 来执行此操作,只需调用两个 drupal api 函数即可。

function print_comment($cid) 
{
    $comment = _comment_load($cid);
    return theme('comment',$comment);
}

No reason to use any sql to do this, two drupal api function calls is all it takes.

function print_comment($cid) 
{
    $comment = _comment_load($cid);
    return theme('comment',$comment);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文