开发者

Drupal: Display a list of contribution (posted content) for each user

开发者 https://www.devze.com 2023-03-05 22:01 出处:网络
I’m searching for a way to display开发者_StackOverflow中文版 on a member profile page, the number of contributions in some content types. Basically it has to display something like this:

I’m searching for a way to display开发者_StackOverflow中文版 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}"));


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消