Wednesday, June 28, 2017 05:00 pm EDT

Enhanced Recent Comments Module

Bri's picture

The drupal core comes with a Recent Comments module, as you probably already know. This module doesn't allow for any configuration options, so I simply added to it. The core module is a block module that simply shows the 10 most recent comments. But what if you only want to show 5 posts, or perhaps only show comments from a particular content type? Below are the changes that i made. The majority of this code came directly from the drupal core comment.module file.

* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
function enhancedrecentcomments_help($path, $arg)
$output = ''; //declare output variable

	switch ($path)
		case "admin/help#enhancedrecentcomments":
			$output = '

'. t('Enhanced Recent comments is an enhanced version of the drupal core block. This one allows you to change the number of links displayed and which content types comments should be displayed.'). '

'; break; } return $output; } /** * Implementation of hook_block(). * * Generates a block with the most recent comments. */ function enhancedrecentcomments_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Enhanced Recent Comments'); return $blocks; } else if ($op == 'view' && user_access('access comments')) { $block['subject'] = t('Recent comments'); //get the options set for which content types to show comments for $typeToShow = variable_get("enhancedrecentcomments_allowed", array()); //loop thru, and add it to a text variable if the value is not 0 foreach($typeToShow as $type) { if($type != "0") { $typeText .= "'".$type."', "; } } //if no content types are selected, don't display the block (optionally u can set teh string to "no comments" or something like that) if(empty($typeText)) { $block['content'] = ''; } else { $block['content'] = theme_enhancedrecentcomments_block(); } return $block; } } /** * Returns a formatted list of recent comments to be displayed in the comment block. * * @return * The comment list HTML. * @ingroup themeable */ function theme_enhancedrecentcomments_block() { $items = array(); $limitnum = variable_get("enhancedrecentcomments_maxdisp" , 5); foreach (enhancedrecentcomments_get_recent($limitnum) as $comment) { $items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)) .'
'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp))). "
"; } if ($items) { return theme('item_list', $items); } } function enhancedrecentcomments_menu() { $items = array(); $items['admin/settings/enhancedrecentcomments'] = array( 'title' => 'Enhanced Recent Comments Settings', 'description' => 'Adjust the recent comments settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('enhancedrecentcomments_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } //create the form for the administration page function enhancedrecentcomments_admin() { $form = array(); $form['enhancedrecentcomments_maxdisp'] = array( '#type' => 'textfield', '#title' => t('Maximum number of comments'), '#default_value' => variable_get('enhancedrecentcomments_maxdisp', 5), '#size' => 2, '#maxlength' => 2, '#description' => t("The maximum number of comments to display in the block."), '#required' => TRUE, ); $form['enhancedrecentcomments_allowed'] = array( '#type' => 'checkboxes', '#title' => t('Include these content types'), '#default_value' => variable_get('enhancedrecentcomments_allowed', array()), '#options' => array_map('check_plain', node_get_types('names')), '#description' => t("Check the content types that you want to be included in the recent comments block"), ); return system_settings_form($form); } //validation for the aministrative form function enhancedrecentcomments_admin_validate($form, &$form_state) { $maxdisp = $form_state['values']['enhancedrecentcomments_maxdisp']; if(!is_numeric($maxdisp)) { form_set_error('enhancedrecentcomments_maxdisp', t("You must enter an integer for the maximum number of links")); } else if($maxdisp <= 0) { form_set_error('enhancedrecentcomments_maxdisp', t("Maximum number of links must be positive")); } } /** * Find a number of recent comments. This is done in two steps. * 1. Find the n (specified by $number) nodes that have the most recent * comments. This is done by querying node_comment_statistics which has * an index on last_comment_timestamp, and is thus a fast query. * 2. Loading the information from the comments table based on the nids found * in step 1. * * @param $number * (optional) The maximum number of comments to find. * @return * An array of comment objects each containing a nid, * subject, cid, and timestamp, or an empty array if there are no recent * comments visible to the current user. */ function enhancedrecentcomments_get_recent($number = 10) { // Select the $number nodes (visible to the current user) with the most // recent comments. This is efficient due to the index on // last_comment_timestamp. //get the options set for which content types to show comments for $typeToShow = variable_get("enhancedrecentcomments_allowed", array()); //loop thro, and add it to a text variable if the value is not 0 foreach($typeToShow as $type) { if($type != "0") { $typeText .= "'".$type."', "; } } // search for the last occurence of a comma $pos = strrpos($typeText, ","); //narrow the typetext down to where the last comma $typeText = substr($typeText, 0, $pos); $result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc LEFT OUTER JOIN {node} n on n.nid = nc.nid WHERE nc.comment_count > 0 AND type IN (".$typeText.") ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number); $nids = array(); while ($row = db_fetch_object($result)) { $nids[] = $row->nid; } $comments = array(); if (!empty($nids)) { // From among the comments on the nodes selected in the first query, // find the $number most recent comments. $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', COMMENT_PUBLISHED, 0, $number); while ($comment = db_fetch_object($result)) { $comments[] = $comment; } } return $comments; }


Anonymous's picture

Many thanks for the changes

Many thanks for the changes on this review website. Yes, this really happens sometimes that we want to view only 5 or less than 5 posts but there is no option of configuration then we can’t do anything. But thanks to you that you have added some changes in the modules. Hope it will resolve the things.

Anonymous's picture

The site seems to be

The site seems to be good..!!Thanks for sharing..!!