Dec 12 2012
Dec 12

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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**  
 * 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 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.

Oct 28 2011
ChX
Oct 28

DISQUS is a popular "social commenting" platform. It is integrated with many hosted blog platforms and open source CMSes, including Drupal. A client of ours exported the comments from their old Wordpress blog and then imported them into DISQUS. The problem was that the comments were showing up in the DISQUS dashboard, however, when you clicked their corresponding URLs, these imported comments did not appear in Drupal. While the Drupal module looks for comments on the node/X URLs, DISQUS was storing them at the old Wordpress URL which were implemented as path aliases in this case.

Fixing this was relatively easy as DISQUS has a mapping tool available. You can download the URLs it knows about and then upload a very simple CSV file to change the URLs. To generate the CSV file after you have saved the Disqus URLs into disqus-urls.csv, just run the following script with drush php-script disqusmap.php > map:

<?php
$base = 'http://www.example.com/';
$n = strlen($base);
foreach (file('disqus-urls.csv') as $url) {
  $path = substr(trim($url), $n);
  $source = drupal_get_normal_path($path);
  if ($source != $path) {
    echo "$base$path, $base$source\n";
  }
}

After you have uploaded the file there is nothing to do but wait. As far as I can see, there are no logs, progress reports, or anything that provides status. In this client's case re-mapping worked to solve their missing comments and so hopefully it will work for you as well.

Jun 09 2008
Jun 09

For those of you who don't know what Disqus is, it's a web service that provides a slick enhancement to comments in websites. Usually when you visit a website, you see a discussion going. This discussion usually just takes place on that website. What you're left with is a bunch of different websites with a bunch of different discussions going on. Disqus rethinks this philosophy by bringing all of those different discussions together.

If you're making a comment on a website that uses Disqus, you'll be able to not only do cool things like make posts from your mobile device, reply to threads through email or make voice comments, you'll also be able to see other discussions going on about the same thing from other websites. You can track where you're discussions are taking place, and get email updates when replies are made. You can also subscribe to a user's comments, export your posts, or import new sets of comments. In essence, Disqus is rethinking how discussions are taking place on multiple different websites.

Although the Drupal comment module is pretty nice, it didn't do exactly what I wanted on my own site. Disqus seemed like a nice alternative, so I put together a Disqus Drupal module, which implements the Disqus web service using Drupal. It allows you to add a discussions on any node type (although we could easily extend this to any taxonomy item, any view, etc). The Disqus guys are working hard on adding import features, as well as extending their API, so there are plans to take advantage of those with this module later on, but this is a good start. If you're interested in trying it out, feel free to make a comment on this post saying where you plan to use Disqus. Enjoy the Disqus Drupal module, and feel free to add any additional ideas you may have!

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web