Things you need to know:
- hook_node_view
- hook_query_TAG_alter
- db_or
Step I read code.
Step 1: find function comment_node_view this function define element comment in array render of node.
Step 2 find line 711
$node->content['comments'] = comment_node_page_additions($node);//define comment
Step 3 go to function comment_node_page_additions see code.
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, 50);
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);
$build['pager']['#theme'] = 'pager';
$additions['comments'] = $build;
}
}
Step 4 If user is anonymous $node->comment_count = 0. so condition is false. Don't add comment. (Problem 1)
Step 5 function comment_get_thread
if (!user_access('administer comments')) {
$query->condition('c.status', COMMENT_PUBLISHED);
$count_query->condition('c.status', COMMENT_PUBLISHED);
}
this function return array store cids (all comment of node) with condition status is published (don't get comment of user is anonymous) Problem 2.
Problem solving
Problem 1: Use hook_node_view to set $node->commentcount != 0 (require this function run before function comment_node_view. You can set weigh of module (sure test with value is -1) or name of module start with a or b (not sure)).
/**
* Implements hook_node_view().
*/
function ModuleName_node_view($node, $view_mode, $langcode) {
global $user;
if ($user->uid == 0 && $node->comment_count == 0) {
$node->comment_count = 1;
}
}
Problem 2
You need alter this query to get comment published or current user is author of comment. To do it i need hook_query_TAG_alter.
You can write a custom module with code:
/**
* Implements hook_query_TAG_alter().
*/
function ModuleName_query_comment_filter_alter(QueryAlterableInterface $query){
global $user;
if (!user_access('administer comments')) {
$conditions = &$query->conditions();
foreach ($conditions as $key => $condition) {
if (is_array($condition) && is_string($condition['field'])
&& $condition['field'] == 'c.status'
&& $condition['value'] == COMMENT_PUBLISHED) {
unset($conditions[$key]); // unset condition comment is public
$or = db_or();
$or->condition('c.status', COMMENT_PUBLISHED);
//$or->condition('c.uid', $user->uid); this case for require use login to comment
$or->condition('c.hostname', $user->hostname); // Ip of user
$query->condition($or);
return;
}
}
}
}
NOTE: All the users have the same IP are seeing comment.