PHP 7
Disqus.views.inc Guest on 25th June 2020 03:32:08 AM
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Views integration with the Disqus module.
  6.  */
  7.  
  8. /**
  9.  * Implements hook_views_data_alter().
  10.  */
  11. function disqus_views_data_alter(&$data) {
  12.  // Number of Disqus comments made on the given node.
  13.   $data['node']['disqus_comment_count'] = array(
  14.     'field' => array(
  15.       'title' => t('Disqus Comment Count'),
  16.       'help' => t('The number of Disqus comments made on the post. Note that this will not work in the preview.'),
  17.       'handler' => 'views_handler_field_node_disqus_comment_count',
  18.     ),
  19.   );
  20. }
  21.  
  22. /**
  23.  * Field handler to present the number of Disqus comments on a node.
  24.  */
  25. class views_handler_field_node_disqus_comment_count extends views_handler_field {
  26.   function init(&$view, &$options) {
  27.     parent::init($view, $options);
  28.   }
  29.  
  30.   function query() {
  31.     // Override parent::query() without altering query.
  32.   }
  33.  
  34.   /**
  35.    * When rendering the field.
  36.    */
  37.   function render($values) {
  38.     // Ensure Disqus comments are available on the node user has access to edit this node.
  39.     $node = node_load($values->nid);
  40.     if (user_access('view disqus comments') && isset($node->disqus)) {
  41.       // Extract the Disqus values.
  42.       $disqus = $node->disqus;
  43.       // Build a renderable array for the link.
  44.       $content = array(
  45.         '#theme' => 'link',
  46.         '#text' => t('Comments'),
  47.         '#path' => $disqus['identifier'],
  48.         '#options' => array(
  49.           'fragment' => 'disqus_thread',
  50.           'attributes' => array(
  51.             // Identify the node for Disqus with the unique identifier:
  52.             // http://docs.disqus.com/developers/universal/#comment-count
  53.             'data-disqus-identifier' => $disqus['identifier'],
  54.           ),
  55.           'html' => FALSE,
  56.         ),
  57.       );
  58.  
  59.       /**
  60.        * This attaches disqus.js, which will look for the DOM variable
  61.        * disqusComments which is set below. When found, the disqus javascript
  62.        * api replaces the html element with the attribute:
  63.        * "data-disqus-identifier" and replaces the element with the number of
  64.        * comments on the node.
  65.        */
  66.       $content['#attached'] = array(
  67.         'js' => array(
  68.           array('data' => drupal_get_path('module', 'disqus') . '/disqus.js'),
  69.           array(
  70.             'data' => array('disqusComments' => $disqus['domain']),
  71.             'type' => 'setting',
  72.           ),
  73.         ),
  74.       );
  75.       return drupal_render($content);
  76.     }
  77.   }
  78. }
  79. ?>

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.