How to assign a user a title based on the number of created nodes? I want to set up a system where a user is given a title based on how much content they have created/published. So, for instance, if the user has published:

0 nodes  - Noobie
1+ nodes - Poster
10+ nodes - Frequent poster
20+ nodes - Super Poster
50+ nodes - God`


I'm wondering if there is a way to implement this in a Drupal site. I can think of a way to do it using database queries and a computed field, but I'm wondering if there might be a more efficient way to go about doing it.
I doubt there is a module for this, but hey. Do not let that bring you down, it's actually a great idea for a custom module! If you have a bit of patience, Drupal is very easy to learn once you get past the initial learning curve.

If you need a start, the module developper section and the create a module page on Drupal are great place to start.

There are many, many ways you could do this, but to start I would probably create a small module. Imagine it's called super_usernames, for the purpose of this post. I would just create a folder in sites/all/modules called `super_usernames. That folder would need to  contain only 2 files: 

super_usernames.info and super_usernames.module. The info file is very easy:

name = Super Usernames
description = Gives special credits to usernames depending on number of posts.
core = 7.x
files[] = super_usernames.module


The module file would need at the minimum just one hook to be defined.  Hooks are just plain old php functions that use a simple naming convention in order to be executed by Drupal automatically.

The hook you need here is hook_node_view. Basically you want to add :

/*
 * Implements hook_node_load
 */
function super_usernames_node_view($node, $view_mode){
  $supported_node_types = array('page', 'article', 'blog'); // Supports default content types
  if (in_array($node-&gt;type, $supported_node_types) { 
    // query to get number of nodes this user has published
    $result = db_query("SELECT count(nid) as nodecount from {node} WHERE uid = %d", $node-&gt;uid);
     $row = db_fetch_object($result); 
     $node_count = $row-&gt;nodecount; // Easier to read
     switch ($node_count){
       case $node_count &gt;= 50:
           $node-&gt;super_username = "God";
         break;
       case $node_count &gt;= 20:
          $node-&gt;super_username = "Super Poster";
         break; 
       case $node_count &gt;= 10:
          $node-&gt;super_username = " Frequent Poster";
         break;
       case  $node_count &gt;= 1:  // same as &gt; 0, but who cares...
           $node-&gt;super_username = "Poster";
         break;
       default:
           $node-&gt;super_username = "Noobie";                  
     }
  }
}


Now. After doing this, all your nodes should contain the dynamic value of the number of nodes posted by that node's author.  This would be added on the fly on every page load from content-types articles, and pages.

You still need to have it displayed somewhere, though! This is where you would ideally create a sub-theme (so as not to hack a base theme). But for the purpose of this post, we will simply modify one file: node.tpl.php.

Check the theme you are using, by default it is bartik but the procedure is mostly the same whatever theme you are using.

Go into your themes/bartik/templates directory, open the node.tpl.php file.  Locate where you want to add the super username in the html, and add this code snippet:

&lt;?php if ($node-&gt;super_username): ?&gt;
  &lt;div class="superUsername"&gt;
    &lt;?php print $node-&gt;super_username; ?&gt;
  &lt;/div&gt;
&lt;?php endif; ?&gt;


Now. One last step, you may need to clear the cache for changes to take effect. Devel module makes it easy, Drush makes it even easier. After this, you should have your super usernames dynamically shown on all the content-types defined in the supported_node_types array.

Hope this doesn't sound too complicated, it's actually quite trivial when you go through the process a few times.  Feel free to message me or repost here if you need any help.

Happy coding, cheers! 


PS: This code has been slapped up from memory, nothing is tested but should be pretty close to a fully working example. Your mileage may vary, there may be typos in there.