如何在 Drupal 中设置评论链接主题?

发布于 2024-10-18 15:13:15 字数 315 浏览 1 评论 0原文

我想对启用评论的节点上显示的“添加评论”和“评论”链接进行主题化。我知道有 theme()theme_links() 可以帮助解决此问题,但我不确定如何使用它们。我很确定我想要 theme_links(),因为在这种情况下我需要链接。但具体如何获取评论链接呢?我不想为所有链接设置主题,只想为评论中的链接设置主题。如果有帮助,我的目标是在每个链接旁边添加一个图像。另外,在“评论”旁边,我想包含发布的评论数量。

为了澄清,我想主题化出现在节点上的链接,而不是出现在评论本身上的链接。

I want to theme the "Add Comment" and "Comments" links that are shown on a node that has comments enabled. I know there's theme() and theme_links() that can help with this, but I'm not sure how to use them. I'm pretty sure I want theme_links(), since I'm after links in this case. But how to I get the comment links specifically? I don't want to theme all links, just the ones on the comments. If it helps, my goal is to add an image next to each of these links. Also, next to "Comments" I want to include the number of comments posted.

To clarify, I want to theme the links that appear on the Node, not the links that appear on the Comments themselves.

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

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

发布评论

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

评论(3

扭转时空 2024-10-25 15:13:15

要将图像/图标添加到链接,您可以使用简单的 CSS。此 CSS 将为“添加评论”链接添加一个图标,但也可以对其他链接执行相同的操作(li.comment_delete、li.comment_edit 等)。

ul.links > li.comment_add > a {
  background: url(PATH TO IMAGE) no-repeat;
  padding-left: 20px;  /* Change to compensate for size of image */
}

要添加节点上的评论数量,您可以使用函数 comment_num_all($node->nid)。例如,如果您想将评论数量添加到“添加评论”链接,您可以将隐藏的 DIV 添加到 node.tpl.php(或每个内容类型模板)和 jQuery 来编辑链接文本:

<div id="num-comments" style="display:none;"><?php print comment_num_all($node->nid); ?></div>

jQuery:

$('ul.links > li.comment_add > a').text('Add new comment (' + $('#num-comments').text() + ')');

这不是最优雅的解决方案,但它确实有效。如果您想使用 theme_links() 我认为您必须创建一个自定义模块。

编辑:
另一种选择是创建自定义模块。这不使用 theme_links(),而是使用 hook_link_alter()。这是一个小示例模块,用于更改“添加新评论”链接的标题、添加图标并包含附加到节点的当前评论数量:(将 MYMODULE_NAME 的每个实例替换为您为模块选择的名称)

步骤1:创建一个名为 MYMODULE_NAME.info 的文件并添加:

name = "MYMODULE_NAME"
description = "Change the appearance of links that appear on nodes"
core = 6.x

步骤 2:创建一个名为 MYMODULE_NAME.module 的文件并添加:

<?php

  /**
   * Implementation of hook_link_alter
   */
  function MYMODULE_NAME_link_alter(&$links, $node){
    if (!empty($links['comment_add'])) {
      // Get number of comments for node
      $num_comments = db_result(db_query('
        SELECT comment_count 
        FROM {node_comment_statistics} 
        WHERE nid = %d
      ', $node->nid));

      // Set "Add new comment" link text
      $links['comment_add']['title'] = '<img src="PATH TO ICON"/> ADD COMMENT TEXT (' . $num_comments . ')';

      // Allow HTML in the link text
      $links['comment_add']['html'] = TRUE;
    }
  }

步骤 3:将这些文件放入名为 MYMODULE_NAME 的文件夹中,将该文件夹放在 site/all/modules 中,然后启用该模块

编辑:查找数组键:
在您的node.tpl.php(或任何其他节点模板)中,您可以添加 links); ?>。这将向您显示要在节点中显示的所有链接信息,并且主数组的键是您将在我的模块中使用的内容。您还可以尝试使用 Firebug/Chrome 开发工具等来查看包含链接的列表项的类(即 ul.links > li.comment_add)。我相信当构建链接时,Drupal 使用数组键作为链接的类。

To add an image/icon to a link, you can use simple CSS. This CSS will add an icon to the "Add Comment" link, but the same could be done for other links as well (li.comment_delete, li.comment_edit, etc).

ul.links > li.comment_add > a {
  background: url(PATH TO IMAGE) no-repeat;
  padding-left: 20px;  /* Change to compensate for size of image */
}

To add the number of comments on a node you can use the function comment_num_all($node->nid). For instance if you would like to add the number of comments to the "Add comment" link, you could add a hidden DIV to the node.tpl.php (or each content type template) and jQuery to edit the link text:

<div id="num-comments" style="display:none;"><?php print comment_num_all($node->nid); ?></div>

jQuery:

$('ul.links > li.comment_add > a').text('Add new comment (' + $('#num-comments').text() + ')');

This is not the most elegant solution, but it works. If you want to use theme_links() I think you would have to create a custom module.

EDIT:
Another option is to create a custom module. This does not use theme_links(), but hook_link_alter() instead. This is a small example module to change the title of the "Add new comment" link, add an icon and include the number of current comments attached to the node: (Replace every instance of MYMODULE_NAME with the name you choose for the module)

STEP 1: Create a file called MYMODULE_NAME.info and add:

name = "MYMODULE_NAME"
description = "Change the appearance of links that appear on nodes"
core = 6.x

STEP 2: Create file called MYMODULE_NAME.module and add:

<?php

  /**
   * Implementation of hook_link_alter
   */
  function MYMODULE_NAME_link_alter(&$links, $node){
    if (!empty($links['comment_add'])) {
      // Get number of comments for node
      $num_comments = db_result(db_query('
        SELECT comment_count 
        FROM {node_comment_statistics} 
        WHERE nid = %d
      ', $node->nid));

      // Set "Add new comment" link text
      $links['comment_add']['title'] = '<img src="PATH TO ICON"/> ADD COMMENT TEXT (' . $num_comments . ')';

      // Allow HTML in the link text
      $links['comment_add']['html'] = TRUE;
    }
  }

STEP 3: Put these files in a folder called MYMODULE_NAME, place the folder in sites/all/modules, and enable the module

EDIT: To find array keys:
in your node.tpl.php (or any other node template) you can add <?php print_r($node->links); ?>. This will show you all of the link info to be displayed in the node, and the keys of the main array are what you would use in my module. You can also try using Firebug/Chrome Dev Tools, etc to look at the class of the list item containing the link (ie ul.links > li.comment_add). I believe when the links are constructed, Drupal uses the array key as a class for the link.

旧情别恋 2024-10-25 15:13:15

我认为最简单的方法是覆盖主题中的 comment.tpl.php 文件。您可以复制 /themes/garland 中的一个作为基础。

I think that the simplest thing to do would be to override the comment.tpl.php file in your theme. You can copy the one in /themes/garland to use as a base.

寂寞花火° 2024-10-25 15:13:15

我使用了 http://drupal.org/node/352020 中的这种技术来创建相同的类型对于链接,您需要向模块添加一个预处理挂钩以访问 $links 数组:

function yourmodule_preprocess_comment (&$variables) {
  $comment = $variables['comment'];

  //load links for current comment
  $links = comment_links($comment, FALSE);

  //code to alter the links array

  //reset the links HTML
  $variables['links'] = theme('links', $links);
}

I've used this technique from http://drupal.org/node/352020 for creating the same kind of links, you want to add a preprocess hook to your module to access the $links array:

function yourmodule_preprocess_comment (&$variables) {
  $comment = $variables['comment'];

  //load links for current comment
  $links = comment_links($comment, FALSE);

  //code to alter the links array

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