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

时间:2022-09-12 08:44:10

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)

博客(10)文章(10)问题(19)评论(30)提示(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.

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

Any help og suggestions would be very much appreciated.

任何帮助og建议将非常感谢。

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}"));

2 个解决方案

#1


1  

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:

将此代码段插入主题文件夹中的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;
}

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

现在,在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; ?>

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.

这只是您可以做的一般概念。您还可以为要查找/显示的内容类型添加限制,检查用户查看这些统计信息的权限,使用CSS更改统计信息的外观等。

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.

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

#2


1  

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.

鉴于您的简单要求以及您掌握SQL语句的事实,我会说只是使用它。没有理由为您的网站添加另一个模块,并且为了单个查询而影响它的性能。

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.

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

#1


1  

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:

将此代码段插入主题文件夹中的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;
}

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

现在,在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; ?>

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.

这只是您可以做的一般概念。您还可以为要查找/显示的内容类型添加限制,检查用户查看这些统计信息的权限,使用CSS更改统计信息的外观等。

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.

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

#2


1  

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.

鉴于您的简单要求以及您掌握SQL语句的事实,我会说只是使用它。没有理由为您的网站添加另一个模块,并且为了单个查询而影响它的性能。

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.

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