You are here

node_comment_block.module in Node Comment Block 7

Same filename and directory in other branches
  1. 7.2 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') {
    if (arg(0) == 'node' && is_numeric($nid = arg(1)) && !arg(2)) {
      $node = node_load($nid);
      if ($node->comment != 0) {

        // This should be NULL otherwise there will be duplicate h2 elements.
        $block['subject'] = NULL;
        $block['content'] = '';
        global $_node_comment_block_comments;
        if ($comments = $_node_comment_block_comments) {
          $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'] .= render($comments);
        }
      }
    }
  }
  return $block;
}

/**
 * Implements hook_node_view().
 */
function node_comment_block_node_view($node, $view_mode) {

  // Store the comments in a global variable so that they can be rendered later.
  global $_node_comment_block_comments;
  $_node_comment_block_comments = isset($node->content['comments']) ? $node->content['comments'] : '';
}

/**
 * Implements hook_preprocess_HOOK() for nodes.
 */
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),
  );
  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']);
  }
}