Skip to main content

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

Bill Renk | Software Architect

December 12, 2012


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.

/**  
 * Implements hook_schema().  
 */
function disqus_comment_count_schema() {
  $schema = array();
  $schema['disqus_comment_count'] = array(
    'description' => 'Stores counts from disqus',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'not null' => true,
        'description' => 'nid of related node',
      ),
      'count' => array(
        'type' => 'int',
        'not null' => true,
        'description' => 'number of reads',
      ),
    ),
    'indexes' => array(
      'disqus_comment_count_nid' => array('nid'),
      'disqus_comment_count_count' => array('count'),
    ),
    'primary key' => array('nid'),
  );
  return $schema;
}

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

function disqus_comment_count_views_api() {
  return array(
    'api' => 3,
  );
}

/**  
 * Implements hook_cron().
 */
function disqus_comment_count_cron() {  
  // Assumes the Disqus module is installed   
  // Could be made into module admin settings if it is not   
  $secret_key = check_plain(variable_get('disqus_secretkey', ''));  
  $forum = check_plain(variable_get('disqus_domain', ''));

  // According to Disqus api: disqus.com/api/docs/threads/listPopular/   
  // acceptable interval options are: 1h, 6h, 12h, 1d, 3d, 7d, 30d, 90d   
  $interval = '1d'; // hard-coding one day, but could make this an admin setting   

  // Using the Disqus php api downloaded to sites/all/libraries from   
  // github.com/disqus/disqus-php   $path = libraries_get_path('disqusapi');    
  require($path . '/disqusapi/disqusapi.php');  
  $disqus = new DisqusAPI($secret_key);  
  $data = array(); //will hold return data    

  try {
    $data = $disqus->threads->listPopular(array(      
      'forum' => $forum,        
      'interval' => $interval,    
    ));  
  }  
  catch (Exception $e) {    
    // Log or throw exception   
  }    

  if (!empty($data)) {    
    // Clear out the table and insert new rows     
    db_query('delete from {disqus_comment_count}');      
    foreach ($data as $comment_info) {
      $nid = str_replace('node/', '', $comment_info->identifiers[ 0 ]);      
      $record = array('nid' => $nid, 'count' => $comment_info->posts);      
      drupal_write_record('disqus_comment_count', $record);    
    }  
  }
}
Finally, add a Disqus count sort option for Views in the module’s .views.inc file:
/**
 * Implements hook_views_data(). 
 */
function disqus_comment_count_views_data() {
  $data = array();
  $data['disqus_comment_count']['table']['group'] = t('Disqus Comments');  
  $data['disqus_comment_count']['table']['join'] = array(
    'node' => array(      
      'table' => 'disqus_comment_count',
      'left_field' => 'nid',
      'field' => 'nid',
      'type' => 'left outer',    
    )  
  );    
  $data['disqus_comment_count']['count'] = array(    
    'title' => t('Comment Count'),    
    'help' => t('Number of Disqus posts on a node.'),    
    'sort' => array(      
      'handler' => 'views_handler_sort',    
     ),  
  );    
  return $data;
}

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.


Recommended Next
Development
A Developer's Guide For Contributing To Drupal
Black pixels on a grey background
Development
3 Steps to a Smooth Salesforce Integration
Black pixels on a grey background
Development
Drupal 8 End of Life: What You Need To Know
Woman working on a laptop
Jump back to top