Search code examples
sqldrupaldrupal-6drupal-views

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


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

Solution

  • 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.