You are here

ajax_comments_handler_field_list_comments.inc in AJAX Comments 7

Definition of ajax_comments_handler_field_list_comments. TODO: Comments per page as setting TODO: Flat or nested as option

File

views/ajax_comments_handler_field_list_comments.inc
View source
<?php

/**
 * @file
 * Definition of ajax_comments_handler_field_list_comments. 
 * TODO: Comments per page as setting
 * TODO: Flat or nested as option
 */

/**
 * Handler for showing an ajaxified list of comments.
 */
class ajax_comments_handler_field_list_comments extends views_handler_field_entity {
  function option_definition() {
    $options = parent::option_definition();
    $options['ajax_comments_display_add_comment_form'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {
    $form['ajax_comments_display_add_comment_form'] = array(
      '#title' => t('Show Add Comment form'),
      '#description' => t("Enable to display the Add Comment form below the list of comments."),
      '#type' => 'checkbox',
      '#default_value' => $this->options['ajax_comments_display_add_comment_form'],
    );
    parent::options_form($form, $form_state);
  }
  function pre_render(&$values) {
    parent::pre_render($values);

    // Load necessary AJAX libraries.
    drupal_add_js(drupal_get_path('module', 'ajax_comments') . '/ajax_comments.js');
    drupal_add_library('system', 'drupal.ajax');
    if (module_exists('ajax_comments_nodejs')) {
      foreach ($values as $node) {
        nodejs_send_content_channel_token('ajax_comments_nodejs_' . $node->nid);
      }
      drupal_add_js(drupal_get_path('module', 'ajax_comments_nodejs') . '/nodejs.ajaxcomments.js', array(
        'type' => 'file',
      ));
      drupal_add_css(drupal_get_path('module', 'ajax_comments_nodejs') . '/ajax_comments_nodejs.css', array(
        'type' => 'file',
      ));
    }
  }

  /**
   * Render the list of comments
   * modified to add the ajax_comments wrapper and the add comment form option.
   */
  function render($values) {
    if ($node = $this
      ->get_value($values)) {

      // Init with dummy Comment. For explanation look at ajax_comments_process_node().
      $result = array(
        'comments' => array(
          'dummyComment' => array(
            '#prefix' => '<div class="ajax-comment-wrapper ajax-comment-dummy-comment" style="display:none">',
            '#type' => 'item',
            '#markup' => '',
            '#suffix' => '</div>',
          ),
        ),
      );

      // Only attempt to render comments if the node has visible comments.
      // Unpublished comments are not included in $node->comment_count, so show
      // comments unconditionally if the user is an administrator.
      if ($node->comment_count && user_access('access comments') || user_access('administer comments')) {
        $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
        $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 150);
        if ($cids = comment_get_thread($node, $mode, $comments_per_page)) {
          $comments = comment_load_multiple($cids);
          comment_prepare_thread($comments);
          $build = comment_view_multiple($comments, $node);

          // TODO: There seems to be a problem with nested pagers :/

          //$build['pager']['#theme'] = 'pager';
          $result['comments'] += $build;
        }
      }

      // Append comment form if needed.
      if (user_access('post comments') && $node->comment == COMMENT_NODE_OPEN && (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW && $this->options['ajax_comments_display_add_comment_form'])) {
        $result['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) array(
          'nid' => $node->nid,
        ));
      }

      // Theme comments
      $result += array(
        '#theme' => 'comment_wrapper__node_' . $node->type,
        '#node' => $node,
        'comments' => array(),
        'comment_form' => array(),
      );
      return $result;
    }
  }

}

Classes

Namesort descending Description
ajax_comments_handler_field_list_comments Handler for showing an ajaxified list of comments.