Drupal:显示每个用户的贡献列表(发布的内容)

发布于 2024-11-06 22:26:47 字数 601 浏览 0 评论 0原文

我正在寻找一种在会员个人资料页面上显示某些内容类型的贡献数量的方法。基本上它必须显示类似这样的内容:

博客(10)
文章(10)
问题(19)
评论(30)
提示(3)

我已经安装了一些不同的模块(例如“用户统计信息”),我认为这些模块可以帮助我,但尚未成功。

我想知道通过开始获取 uid 并将其硬编码到我的模板文件中是否最简单,然后使用我想要显示的内容类型运行一些查询,但我也不确定如何做到这一点。

任何帮助和建议将非常感激。

真诚 - 梅斯蒂卡

编辑:
我找到了一个通过查询每种内容类型手动执行此操作的解决方案,但我仍然对更优雅、更流畅的解决方案非常感兴趣。

我使用这段代码:

global $user;
$userid = $user->uid;
$blog_count  = db_result(db_query("SELECT COUNT(0) AS num FROM {node} n where n.type = 'blog' AND status = 1 AND n.uid = {$userid}"));

I’m searching for a way to display on a member profile page, the number of contributions in some content types. Basically it has to display something like this:

Blog(10)
Articles(10)
Questions(19)
Comments(30)
Tips(3)

I’ve installed some different modules (like “user stats”) that I though could help me but haven’t been successful.

I’m wondering if it would be easiest just to hard-code it into my template file by starting taking the uid and just run some queries with the content types I want to display but I’m not sure on how to do that either.

Any help og suggestions would be very much appreciated.

Sincere
- Mestika

Edit:
I found a solution to do it manually with a query for each content type but I'm still very interested in a solution that's more elegant and smoother.

I use this code:

global $user;
$userid = $user->uid;
$blog_count  = db_result(db_query("SELECT COUNT(0) AS num FROM {node} n where n.type = 'blog' AND status = 1 AND n.uid = {$userid}"));

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

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

发布评论

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

评论(2

勿忘初心 2024-11-13 22:26:47

如果您使用核心 Profile 模块,则可以使用如下所示的内容。它将显示正在查看其个人资料的用户创建的节点。作为一项额外的好处,它只需要执行一个自定义数据库查询。

将此代码片段插入主题文件夹中的 template.php 中,并将“THEMENAME”更改为主题的名称:

function THEMENAME_preprocess_user_profile(&$variables) {
  // Information about user profile being viewed
  $account = $variables['account'];

  // Get info on all content types
  $content_types = node_get_types('names');

  // Get node counts for all content types for current user
  $stats = array();
  $node_counts = db_query('SELECT type, COUNT(type) AS num FROM {node} WHERE status = 1 AND uid = %d GROUP BY type', $account->uid);
  while ($row = db_fetch_array($node_counts)) {
    $stats[] = array(
      'name' => $content_types[$row['type']],
      'type' => $row['type'],
      'num' => $row['num'],
    );
  }
  $variables['node_stats'] = $stats;
}

现在,在 user-profile.tpl.php 中可以添加一些内容类似于:

// If user has created content, display stats
<?php if (count($node_stats) > 0): ?>

  // For each content type, display a DIV with name and number of nodes
  <?php foreach ($node_stats as $value): ?>
    <div><?php print $value['name']; ?> (<?php print $value['num']; ?>)</div>
  <?php endforeach; ?>

// Message to show for user that hasn't created any content
<?php else: ?>
  <?php print $account->name; ?> has not created any content.
<?php endif; ?>

这只是您可以做什么的一般想法。您还可以对您查找/显示的内容类型添加限制、检查用户查看这些统计信息的权限、使用 CSS 更改统计信息的外观等。

如果您使用 内容配置文件,您可以使用 THEMENAME_preprocess_node() 并在执行此代码之前检查该节点是否为配置文件节点。

If you are using the core Profile module, you could use something like below. It will show the nodes created by the user whose profile is being viewed. As an added benefit, it only needs to execute one custom database query.

Insert this snippet into template.php in your theme's folder and change "THEMENAME" to the name of your theme:

function THEMENAME_preprocess_user_profile(&$variables) {
  // Information about user profile being viewed
  $account = $variables['account'];

  // Get info on all content types
  $content_types = node_get_types('names');

  // Get node counts for all content types for current user
  $stats = array();
  $node_counts = db_query('SELECT type, COUNT(type) AS num FROM {node} WHERE status = 1 AND uid = %d GROUP BY type', $account->uid);
  while ($row = db_fetch_array($node_counts)) {
    $stats[] = array(
      'name' => $content_types[$row['type']],
      'type' => $row['type'],
      'num' => $row['num'],
    );
  }
  $variables['node_stats'] = $stats;
}

Now, in user-profile.tpl.php can add something similar to:

// If user has created content, display stats
<?php if (count($node_stats) > 0): ?>

  // For each content type, display a DIV with name and number of nodes
  <?php foreach ($node_stats as $value): ?>
    <div><?php print $value['name']; ?> (<?php print $value['num']; ?>)</div>
  <?php endforeach; ?>

// Message to show for user that hasn't created any content
<?php else: ?>
  <?php print $account->name; ?> has not created any content.
<?php endif; ?>

This is just a general idea of what you can do. You can also add restrictions to the content types you look for/display, check permissions for users to see these stats, use CSS to change the look of the stats, etc.

If you are using Content Profile, you could use THEMENAME_preprocess_node() and check that the node is a profile node before executing this code.

永言不败 2024-11-13 22:26:47

考虑到您的简单要求以及您手头有 SQL 语句的事实,我会说直接使用它。没有理由为了单个查询而向您的站点添加另一个模块并影响其性能。

也就是说,从“关注点分离”的角度来看,您不应该只是将此 SQL 放入模板中。相反,您应该使用 template.php 文件中的预处理函数将其结果添加到可用变量列表中,将其范围限制在您需要的位置,这样您就不会在任何页面而是适当的个人资料页面。

Given your simple requirement and the fact that you have the SQL statement in-hand, I'd say just use that. There's no reason to add yet another module to your site and impact it's performance for the sake of a single query.

That said, from a "separation of concerns" standpoint, you shouldn't just drop this SQL in your template. Instead, you should add its result to the list of available variables using a preprocess function in your template.php file, limiting its scope to where you need it so you're not running this database query on any pages but the appropriate profile page.

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