You are here

node_comment_block.module in Node Comment Block 7.2

Same filename and directory in other branches
  1. 7 node_comment_block.module

Moves the comments for a node into a block.

File

node_comment_block.module
View source
<?php

/**
 * @file
 * Moves the comments for a node into a block.
 */

/**
 * Implements hook_block_info().
 */
function node_comment_block_block_info() {
  $blocks['node_comments'] = array(
    'info' => t('Node comments'),
  );
  $blocks['node_comments_secondary'] = array(
    'info' => t('Node comments secondary block'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function node_comment_block_block_view($delta = '') {
  $block = array();
  if ($delta == 'node_comments' || $delta == 'node_comments_secondary') {
    module_load_include('inc', 'node_comment_block', 'includes/node_comment_block.controller');

    // Check if the current path is a node path, and extract the node ID.
    if (!($node = NodeCommentBlock::getNode(current_path()))) {
      return array();
    }
    if ($node->comment == 0) {
      return array();
    }
    $block['content'] = array();
    if ($comments = NodeCommentBlock::getComments($node)) {
      $show_form = variable_get('node_comment_block_show_form_' . $delta, 1);
      $show_comments = variable_get('node_comment_block_show_comments_' . $delta, 1);
      if (!$show_form) {
        unset($comments['comment_form']);
      }
      if (!$show_comments) {
        $comments['comments'] = array();
      }
      if ((!$show_comments || empty($comments['comments'])) && !$show_form) {
        return array();
      }
      $block['content'] = $comments;
      return $block;
    }

    // Show the login or register text to anonymous users if the setting is
    // enabled and there are no comments to display.
    $show_anonymous = variable_get('node_comment_block_show_anonymous_' . $delta, 0) == 1;
    if (user_is_anonymous() && !user_access('post comments') && $show_anonymous) {
      $block['subject'] = t('Comments');
      $block['content'] = theme('comment_post_forbidden', array(
        'node' => $node,
      ));
    }
  }
  return $block;
}

/**
 * Implements hook_node_view().
 */
function node_comment_block_node_view($node, $view_mode) {
  module_load_include('inc', 'node_comment_block', 'includes/node_comment_block.controller');
  NodeCommentBlock::setComments($node);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function node_comment_block_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->comment !== 0 && $variables['view_mode'] == 'full') {
    if (isset($variables['content']['comments'])) {
      unset($variables['content']['comments']);
      drupal_add_css(drupal_get_path('module', 'node_comment_block') . '/node-comment-block.css');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for block_admin_configure.
 */
function node_comment_block_form_block_admin_configure_alter(&$form, &$form_state) {
  if (isset($form['delta'])) {
    if ($form['delta']['#value'] == 'node_comments' || $form['delta']['#value'] == 'node_comments_secondary') {
      $form['settings']['title']['#disabled'] = TRUE;
      $form['settings']['title']['#description'] = t('The title for this block cannot be overridden.');
    }
  }
}

/**
 * Implements hook_block_configure.
 */
function node_comment_block_block_configure($delta = '') {
  $form['show_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Comment Form?'),
    '#default_value' => variable_get('node_comment_block_show_form_' . $delta, 1),
  );
  $form['show_comments'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Comments?'),
    '#default_value' => variable_get('node_comment_block_show_comments_' . $delta, 1),
  );
  $form['show_anonymous'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show login and register links to anonymous users if there are no comments'),
    '#default_value' => variable_get('node_comment_block_show_anonymous_' . $delta, 0),
  );
  return $form;
}

/**
 * Implements hook_block_save.
 */
function node_comment_block_block_save($delta = '', $edit = array()) {

  // This example comes from node.module.
  if ($delta == 'node_comments' || $delta == 'node_comments_secondary') {
    variable_set('node_comment_block_show_form_' . $delta, $edit['show_form']);
    variable_set('node_comment_block_show_comments_' . $delta, $edit['show_comments']);
    variable_set('node_comment_block_show_anonymous_' . $delta, $edit['show_anonymous']);
  }
}