Getting Most Commented Articles Using Disqus to use as a Most Popular Widget

Most Popular blocks are a pretty common requirement. One nice solution for Drupal is the Drupal Most Popular module.

Bill Renk
#Drupal | Posted

Most Popular blocks are a pretty common requirement. One nice solution for Drupal is the Drupal Most Popular module. The Most Popular module provides several sources for your blocks, such as Drupal core Statistics and Comment modules, as well as Google Analytics (see the issue regarding Google Analytics/Reports), Disqus and AddThis, allowing you to create different block types like Most Viewed, Most Commented and Most Shared. The Most Popular module also provides a lot of nice themeing options and allows you to set up tabbed blocks without writing any code. I won’t go into documenting how the module works, as that has already been done. You may decide you just want to use blocks from Views. If you try to create a Most Commented block using the Disqus module, you quickly discover it currently only provides a field for views and not a sort option. Fortunately, this is not difficult to remedy. A simple module can be created to provide this functionality. (To be really creative, going to it: disqus_most_commented.) First, implement hook_schema in the module .install file to add a new table to hold the comment counts.

  1. /**
  2.  * Implements hook_schema().
  3.  */
  4. function disqus_comment_count_schema() {
  5. $schema = array();
  6. $schema['disqus_comment_count'] = array(
  7. 'description' => 'Stores counts from disqus',
  8. 'fields' => array(
  9. 'nid' => array(
  10. 'type' => 'int',
  11. 'not null' => true,
  12. 'description' => 'nid of related node',
  13. ),
  14. 'count' => array(
  15. 'type' => 'int',
  16. 'not null' => true,
  17. 'description' => 'number of reads',
  18. ),
  19. ),
  20. 'indexes' => array(
  21. 'disqus_comment_count_nid' => array('nid'),
  22. 'disqus_comment_count_count' => array('count'),
  23. ),
  24. 'primary key' => array('nid'),
  25. );
  26. return $schema;
  27. }

 

The meat of the .module file could look something like:

  1. function disqus_comment_count_views_api() {
  2.   return array(
  3.     'api' => 3,
  4.   );
  5. }
  6.  
  7. /**  
  8.  * Implements hook_cron().
  9.  */
  10. function disqus_comment_count_cron() {  
  11.   // Assumes the Disqus module is installed  
  12.   // Could be made into module admin settings if it is not  
  13.   $secret_key = check_plain(variable_get('disqus_secretkey', ''));  
  14.   $forum = check_plain(variable_get('disqus_domain', ''));
  15.  
  16.   // According to Disqus api: disqus.com/api/docs/threads/listPopular/  
  17.   // acceptable interval options are: 1h, 6h, 12h, 1d, 3d, 7d, 30d, 90d  
  18.   $interval = '1d'; // hard-coding one day, but could make this an admin setting  
  19.  
  20.   // Using the Disqus php api downloaded to sites/all/libraries from  
  21.   // github.com/disqus/disqus-php   $path = libraries_get_path('disqusapi');    
  22.   require($path . '/disqusapi/disqusapi.php');  
  23.   $disqus = new DisqusAPI($secret_key);  
  24.   $data = array(); //will hold return data    
  25.  
  26.   try {
  27.     $data = $disqus->threads->listPopular(array(      
  28.       'forum' => $forum,        
  29.       'interval' => $interval,    
  30.     ));  
  31.   }  
  32.   catch (Exception $e) {    
  33.     // Log or throw exception  
  34.   }    
  35.  
  36.   if (!empty($data)) {    
  37.     // Clear out the table and insert new rows    
  38.     db_query('delete from {disqus_comment_count}');      
  39.     foreach ($data as $comment_info) {
  40.       $nid = str_replace('node/', '', $comment_info->identifiers[ 0 ]);      
  41.       $record = array('nid' => $nid, 'count' => $comment_info->posts);      
  42.       drupal_write_record('disqus_comment_count', $record);    
  43.     }  
  44.   }
  45. }

 

Finally, add a Disqus count sort option for Views in the module’s .views.inc file:

  1. /**
  2.  * Implements hook_views_data().
  3.  */
  4. function disqus_comment_count_views_data() {
  5.   $data = array();
  6.   $data['disqus_comment_count']['table']['group'] = t('Disqus Comments');  
  7.   $data['disqus_comment_count']['table']['join'] = array(
  8.     'node' => array(      
  9.       'table' => 'disqus_comment_count',
  10.       'left_field' => 'nid',
  11.       'field' => 'nid',
  12.       'type' => 'left outer',    
  13.     )  
  14.   );    
  15.   $data['disqus_comment_count']['count'] = array(    
  16.     'title' => t('Comment Count'),    
  17.     'help' => t('Number of Disqus posts on a node.'),    
  18.     'sort' => array(      
  19.       'handler' => 'views_handler_sort',    
  20.      ),  
  21.   );    
  22.   return $data;
  23. }

The above functions are relatively simple and can certainly be made more robust, but they illustrate a straight-forward means of adding a Disqus comment count sort option for Views.

Bill Renk