如何获取当前登录的用户评论drupal 7

时间:2017-07-20 08:01:29

标签: drupal drupal-7

我需要获取由他评论的当前用户评论详细信息。

5 个答案:

答案 0 :(得分:4)

请尝试以下代码

global $user;
$query = db_select('comments', 'cm');
  $query->fields(cm);
  $query->condition('cm.uid', $user->uid, '=');
  $result = $query->execute();
  while ($chunk = $result->fetchObject()) {
    print $chunk->subject; // comment title
print $chunk->nid; // on which node user enter comment
print $chunk->mail; // email of user
  }

或者您可以使用视图创建列表而不使用手动查询。 goto structure-> views->在显示字段设置中创建新视图并选择注释选项。

谢谢, Ankush

答案 1 :(得分:1)

您可以创建评论的视图(查看页面或视图块)并向用户添加RELATIONSHIPS(评论:作者)并为用户添加CONTEXTUAL FILTERS(作者用户:Uid)

答案 2 :(得分:0)

创建一个视图。

查看cab获取评论,您可以通过传递(当前)用户ID来添加过滤器以仅获取特定用户添加的评论。

答案 3 :(得分:0)

$view = new view();
$view->name = 'comments';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'comment';
$view->human_name = 'Comments';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Comments';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'access comments';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'comment';
/* Relationship: Comment: Content */
$handler->display->display_options['relationships']['nid']['id'] = 'nid';
$handler->display->display_options['relationships']['nid']['table'] = 'comment';
$handler->display->display_options['relationships']['nid']['field'] = 'nid';
$handler->display->display_options['relationships']['nid']['required'] = TRUE;
/* Relationship: Comment: Author */
$handler->display->display_options['relationships']['uid']['id'] = 'uid';
$handler->display->display_options['relationships']['uid']['table'] = 'comment';
$handler->display->display_options['relationships']['uid']['field'] = 'uid';
/* Field: Comment: Title */
$handler->display->display_options['fields']['subject']['id'] = 'subject';
$handler->display->display_options['fields']['subject']['table'] = 'comment';
$handler->display->display_options['fields']['subject']['field'] = 'subject';
$handler->display->display_options['fields']['subject']['label'] = '';
$handler->display->display_options['fields']['subject']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['subject']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Comment: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'comment';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['relationship'] = 'uid';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Comment: Approved */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'comment';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status_node']['id'] = 'status_node';
$handler->display->display_options['filters']['status_node']['table'] = 'node';
$handler->display->display_options['filters']['status_node']['field'] = 'status';
$handler->display->display_options['filters']['status_node']['relationship'] = 'nid';
$handler->display->display_options['filters']['status_node']['value'] = 1;
$handler->display->display_options['filters']['status_node']['group'] = 1;
$handler->display->display_options['filters']['status_node']['expose']['operator'] = FALSE;

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'comments';

答案 4 :(得分:0)

这不是标准功能,但这会提供登录用户的注释。从nid表中将node存放到user id

我收到没有任何主题的登录用户评论:

$nid_result = db_select('node') -> fields('node', array('nid')) -> 
  condition('uid', $puser->uid, '=') -> 
  condition('type', 'user_profile_comment', '=') -> execute(); 
$nid = $nid_resul t-> fetchCol();

$user_detail = array(); 
$result = db_select('comment') -> fields('comment', array('cid')) -> 
  condition('nid', $nid[0], '=') -> orderBy('cid', 'DESC') -> execute(); 
$cids = $result->fetchCol(); 
$comments=comment_load_multiple($cids); 
print_r($comments);