5 * Provide views data and handlers for comment.module.
7 * @ingroup views_module_handlers
11 * Implements hook_views_data().
14 function comment_views_data() {
15 $data['comments']['moved to'] = 'comment';
16 $data['comment']['comment']['moved to'] = array('field_data_comment_body', 'comment_body');
17 $data['comment']['comment']['field']['moved to'] = array('field_data_comment_body', 'comment_body');
18 // Define the base group of this table. Fields that don't
19 // have a group defined will go into this field by default.
20 $data['comment']['table']['group'] = t('Comment');
22 $data['comment']['table']['base'] = array(
24 'title' => t('Comment'),
25 'help' => t("Comments are responses to node content."),
26 'access query tag' => 'comment_access',
28 $data['comment']['table']['entity type'] = 'comment';
30 // Provide a "default relationship" to keep older views from choking.
31 $data['comment']['table']['default_relationship'] = array(
38 // ----------------------------------------------------------------
42 $data['comment']['subject'] = array(
43 'title' => t('Title'),
44 'help' => t('The title of the comment.'),
46 'handler' => 'views_handler_field_comment',
47 'click sortable' => TRUE,
50 'handler' => 'views_handler_filter_string',
53 'handler' => 'views_handler_sort',
56 'handler' => 'views_handler_argument_string',
61 $data['comment']['cid'] = array(
63 'help' => t('The comment ID of the field'),
65 'handler' => 'views_handler_field_comment',
66 'click sortable' => TRUE,
69 'handler' => 'views_handler_filter_numeric',
72 'handler' => 'views_handler_sort',
75 'handler' => 'views_handler_argument_numeric',
79 // name (of comment author)
80 $data['comment']['name'] = array(
81 'title' => t('Author'),
82 'help' => t("The name of the comment's author. Can be rendered as a link to the author's homepage."),
84 'handler' => 'views_handler_field_comment_username',
85 'click sortable' => TRUE,
88 'handler' => 'views_handler_filter_string',
91 'handler' => 'views_handler_sort',
94 'handler' => 'views_handler_argument_string',
99 $data['comment']['homepage'] = array(
100 'title' => t("Author's website"),
101 'help' => t("The website address of the comment's author. Can be rendered as a link. Will be empty if the author is a registered user."),
103 'handler' => 'views_handler_field_url',
104 'click sortable' => TRUE,
107 'handler' => 'views_handler_filter_string',
110 'handler' => 'views_handler_sort',
113 'handler' => 'views_handler_argument_string',
118 $data['comment']['hostname'] = array(
119 'title' => t('Hostname'),
120 'help' => t('Hostname of user that posted the comment.'),
122 'handler' => 'views_handler_field',
123 'click sortable' => TRUE,
126 'handler' => 'views_handler_filter_string',
129 'handler' => 'views_handler_sort',
132 'handler' => 'views_handler_argument_string',
137 $data['comment']['mail'] = array(
138 'title' => t('Mail'),
139 'help' => t('Email of user that posted the comment. Will be empty if the author is a registered user.'),
141 'handler' => 'views_handler_field',
142 'click sortable' => TRUE,
145 'handler' => 'views_handler_filter_string',
148 'handler' => 'views_handler_sort',
151 'handler' => 'views_handler_argument_string',
155 // created (when comment was posted)
156 $data['comment']['created'] = array(
157 'title' => t('Post date'),
158 'help' => t('Date and time of when the comment was created.'),
160 'handler' => 'views_handler_field_date',
161 'click sortable' => TRUE,
164 'handler' => 'views_handler_sort_date',
167 'handler' => 'views_handler_filter_date',
173 if (module_exists('locale')) {
174 $data['comment']['language'] = array(
175 'title' => t('Language'),
176 'help' => t('The language the comment is in.'),
178 'handler' => 'views_handler_field_locale_language',
179 'click sortable' => TRUE,
182 'handler' => 'views_handler_filter_locale_language',
185 'handler' => 'views_handler_argument_locale_language',
188 'handler' => 'views_handler_sort',
193 $data['comments']['timestamp']['moved to'] = array('comment', 'changed');
194 // changed (when comment was last updated)
195 $data['comment']['changed'] = array(
196 'title' => t('Updated date'),
197 'help' => t('Date and time of when the comment was last updated.'),
199 'handler' => 'views_handler_field_date',
200 'click sortable' => TRUE,
203 'handler' => 'views_handler_sort_date',
206 'handler' => 'views_handler_filter_date',
210 $data['comments']['timestamp_fulldate']['moved to'] = array('comment', 'changed_fulldata');
211 $data['comment']['changed_fulldata'] = array(
212 'title' => t('Created date'),
213 'help' => t('Date in the form of CCYYMMDD.'),
215 'field' => 'changed',
216 'handler' => 'views_handler_argument_node_created_fulldate',
220 $data['comments']['timestamp_year_month']['moved to'] = array('comment', 'changed_year_month');
221 $data['comment']['changed_year_month'] = array(
222 'title' => t('Created year + month'),
223 'help' => t('Date in the form of YYYYMM.'),
225 'field' => 'changed',
226 'handler' => 'views_handler_argument_node_created_year_month',
230 $data['comments']['timestamp_year']['moved to'] = array('comment', 'changed_year');
231 $data['comment']['changed_year'] = array(
232 'title' => t('Created year'),
233 'help' => t('Date in the form of YYYY.'),
235 'field' => 'changed',
236 'handler' => 'views_handler_argument_node_created_year',
240 $data['comments']['timestamp_month']['moved to'] = array('comment', 'changed_month');
241 $data['comment']['changed_month'] = array(
242 'title' => t('Created month'),
243 'help' => t('Date in the form of MM (01 - 12).'),
245 'field' => 'changed',
246 'handler' => 'views_handler_argument_node_created_month',
250 $data['comments']['timestamp_day']['moved to'] = array('comment', 'changed_day');
251 $data['comment']['changed_day'] = array(
252 'title' => t('Created day'),
253 'help' => t('Date in the form of DD (01 - 31).'),
255 'field' => 'changed',
256 'handler' => 'views_handler_argument_node_created_day',
260 $data['comments']['timestamp_week']['moved to'] = array('comment', 'changed_week');
261 $data['comment']['changed_week'] = array(
262 'title' => t('Created week'),
263 'help' => t('Date in the form of WW (01 - 53).'),
265 'field' => 'changed',
266 'handler' => 'views_handler_argument_node_created_week',
270 // status (approved or not)
271 $data['comment']['status'] = array(
272 'title' => t('Approved'),
273 'help' => t('Whether the comment is approved (or still in the moderation queue).'),
275 'handler' => 'views_handler_field_boolean',
276 'click sortable' => TRUE,
277 'output formats' => array(
278 'approved-not-approved' => array(t('Approved'), t('Not Approved')),
282 'handler' => 'views_handler_filter_boolean_operator',
283 'label' => t('Approved comment'),
287 'handler' => 'views_handler_sort',
291 // link to view comment
292 $data['comment']['view_comment'] = array(
294 'title' => t('View link'),
295 'help' => t('Provide a simple link to view the comment.'),
296 'handler' => 'views_handler_field_comment_link',
300 // link to edit comment
301 $data['comment']['edit_comment'] = array(
303 'title' => t('Edit link'),
304 'help' => t('Provide a simple link to edit the comment.'),
305 'handler' => 'views_handler_field_comment_link_edit',
309 // link to delete comment
310 $data['comment']['delete_comment'] = array(
312 'title' => t('Delete link'),
313 'help' => t('Provide a simple link to delete the comment.'),
314 'handler' => 'views_handler_field_comment_link_delete',
319 // link to approve comment
320 $data['comment']['approve_comment'] = array(
322 'title' => t('Approve link'),
323 'help' => t('Provide a simple link to approve the comment.'),
324 'handler' => 'views_handler_field_comment_link_approve',
328 // link to reply to comment
329 $data['comment']['replyto_comment'] = array(
331 'title' => t('Reply-to link'),
332 'help' => t('Provide a simple link to reply to the comment.'),
333 'handler' => 'views_handler_field_comment_link_reply',
337 $data['comment']['thread'] = array(
339 'title' => t('Depth'),
340 'help' => t('Display the depth of the comment if it is threaded.'),
341 'handler' => 'views_handler_field_comment_depth',
344 'title' => t('Thread'),
345 'help' => t('Sort by the threaded order. This will keep child comments together with their parents.'),
346 'handler' => 'views_handler_sort_comment_thread',
350 $data['comment']['nid'] = array(
352 'help' => t('The node ID to which the comment is a reply to.'),
353 'relationship' => array(
354 'title' => t('Content'),
355 'help' => t('The content to which the comment is a reply to.'),
357 'base field' => 'nid',
358 'handler' => 'views_handler_relationship',
359 'label' => t('Content'),
362 'handler' => 'views_handler_filter_numeric',
365 'handler' => 'views_handler_argument_numeric',
368 'handler' => 'views_handler_field_numeric',
372 $data['comment']['uid'] = array(
373 'title' => t('Author uid'),
374 'help' => t('If you need more fields than the uid add the comment: author relationship'),
375 'relationship' => array(
376 'title' => t('Author'),
377 'help' => t("The User ID of the comment's author."),
379 'base field' => 'uid',
380 'handler' => 'views_handler_relationship',
381 'label' => t('author'),
384 'handler' => 'views_handler_filter_numeric',
387 'handler' => 'views_handler_argument_numeric',
390 'handler' => 'views_handler_field_user',
394 $data['comment']['pid'] = array(
395 'title' => t('Parent CID'),
396 'help' => t('The Comment ID of the parent comment.'),
398 'handler' => 'views_handler_field',
400 'relationship' => array(
401 'title' => t('Parent comment'),
402 'help' => t('The parent comment.'),
404 'base field' => 'cid',
405 'handler' => 'views_handler_relationship',
406 'label' => t('Parent comment'),
410 // ----------------------------------------------------------------------
411 // node_comment_statistics table
414 $data['node_comment_statistics']['table']['group'] = t('Content');
417 $data['node_comment_statistics']['table']['join'] = array(
418 //...to the node table
421 'left_field' => 'nid',
426 // last_comment_timestamp
427 $data['node_comment_statistics']['last_comment_timestamp'] = array(
428 'title' => t('Last comment time'),
429 'help' => t('Date and time of when the last comment was posted.'),
431 'handler' => 'views_handler_field_last_comment_timestamp',
432 'click sortable' => TRUE,
435 'handler' => 'views_handler_sort_date',
438 'handler' => 'views_handler_filter_date',
442 // last_comment_name (author's name)
443 $data['node_comment_statistics']['last_comment_name'] = array(
444 'title' => t("Last comment author"),
445 'help' => t('The name of the author of the last posted comment.'),
447 'handler' => 'views_handler_field_ncs_last_comment_name',
448 'click sortable' => TRUE,
449 'no group by' => TRUE,
452 'handler' => 'views_handler_sort_ncs_last_comment_name',
453 'no group by' => TRUE,
458 $data['node_comment_statistics']['comment_count'] = array(
459 'title' => t('Comment count'),
460 'help' => t('The number of comments a node has.'),
462 'handler' => 'views_handler_field_numeric',
463 'click sortable' => TRUE,
466 'handler' => 'views_handler_filter_numeric',
469 'handler' => 'views_handler_sort',
472 'handler' => 'views_handler_argument',
476 // last_comment_timestamp
477 $data['node_comment_statistics']['last_updated'] = array(
478 'title' => t('Updated/commented date'),
479 'help' => t('The most recent of last comment posted or node updated time.'),
481 'handler' => 'views_handler_field_ncs_last_updated',
482 'click sortable' => TRUE,
483 'no group by' => TRUE,
486 'handler' => 'views_handler_sort_ncs_last_updated',
487 'no group by' => TRUE,
490 'handler' => 'views_handler_filter_ncs_last_updated',
494 $data['node_comment_statistics']['cid'] = array(
495 'title' => t('Last comment CID'),
496 'help' => t('Display the last comment of a node'),
497 'relationship' => array(
498 'title' => t('Last Comment'),
499 'help' => t('The last comment of a node.'),
500 'group' => t('Comment'),
502 'base field' => 'cid',
503 'handler' => 'views_handler_relationship',
504 'label' => t('Last Comment'),
509 $data['node_comment_statistics']['last_comment_uid'] = array(
510 'title' => t('Last comment uid'),
511 'help' => t('The User ID of the author of the last comment of a node.'),
512 'relationship' => array(
513 'title' => t('Last comment author'),
515 'base field' => 'uid',
516 'handler' => 'views_handler_relationship',
517 'label' => t('Last comment author'),
520 'handler' => 'views_handler_filter_numeric',
523 'handler' => 'views_handler_argument_numeric',
526 'handler' => 'views_handler_field_numeric',
534 * Use views_data_alter to add items to the node table that are
535 * relevant to comments.
537 function comment_views_data_alter(&$data) {
539 $data['node']['new_comments'] = array(
540 'title' => t('New comments'),
541 'help' => t('The number of new comments on the node.'),
543 'handler' => 'views_handler_field_node_new_comments',
544 'no group by' => TRUE,
548 $data['node']['comments_link'] = array(
550 'title' => t('Add comment link'),
551 'help' => t('Display the standard add comment link used on regular nodes, which will only display if the viewing user has access to add a comment.'),
552 'handler' => 'views_handler_field_comment_node_link',
556 // Comment status of the node
557 $data['node']['comment'] = array(
558 'title' => t('Comment status'),
559 'help' => t('Whether comments are enabled or disabled on the node.'),
561 'handler' => 'views_handler_field_node_comment',
562 'click sortable' => TRUE,
565 'handler' => 'views_handler_sort',
568 'handler' => 'views_handler_filter_node_comment',
572 $data['node']['uid_touch'] = array(
573 'title' => t('User posted or commented'),
574 'help' => t('Display nodes only if a user posted the node or commented on the node.'),
577 'name table' => 'users',
578 'name field' => 'name',
579 'handler' => 'views_handler_argument_comment_user_uid',
580 'no group by' => TRUE,
584 'name table' => 'users',
585 'name field' => 'name',
586 'handler' => 'views_handler_filter_comment_user_uid'
590 $data['node']['cid'] = array(
591 'title' => t('Comments of the node'),
592 'help' => t('Relate all comments on the node. This will create 1 duplicate record for every comment. Usually if you need this it is better to create a comment view.'),
593 'relationship' => array(
594 'group' => t('Comment'),
595 'label' => t('Comments'),
597 'base field' => 'nid',
598 'relationship field' => 'nid',
599 'handler' => 'views_handler_relationship',
606 * Implements hook_views_plugins().
608 function comment_views_plugins() {
613 'title' => t('Comment'),
614 'help' => t('Display the comment with standard comment view.'),
615 'handler' => 'views_plugin_row_comment_view',
616 'theme' => 'views_view_row_comment',
617 'path' => drupal_get_path('module', 'views') . '/modules/comment', // not necessary for most modules
618 'base' => array('comment'), // only works with 'comment' as base.
619 'uses options' => TRUE,
621 'help topic' => 'style-comment',
623 'comment_rss' => array(
624 'title' => t('Comment'),
625 'help' => t('Display the comment as RSS.'),
626 'handler' => 'views_plugin_row_comment_rss',
627 'theme' => 'views_view_row_rss',
628 'path' => drupal_get_path('module', 'views') . '/modules/comment', // not necessary for most modules
629 'base' => array('comment'), // only works with 'comment' as base.
630 'uses options' => TRUE,
632 'help topic' => 'style-comment-rss',
639 * Template helper for theme_views_view_row_comment
641 function template_preprocess_views_view_row_comment(&$vars) {
642 $options = $vars['options'];
643 $view = &$vars['view'];
644 $plugin = &$view->style_plugin->row_plugin;
645 $comment = $plugin->comments[$vars['row']->{$vars['field_alias']}];
646 $node = $plugin->nodes[$comment->nid];
647 // Put the view on the node so we can retrieve it in the preprocess.
648 $node->view = &$view;
650 $build = comment_view_multiple(array($comment->cid => $comment), $node, $plugin->options['view_mode']);
651 // If we're displaying the comments without links, remove them from the
652 // renderable array. There is no way to avoid building them in the first
653 // place (see comment_build_content()).
654 if (empty($options['links'])) {
655 foreach ($build as $cid => &$comment_build) {
656 if (isset($comment_build['links'])) {
657 unset($comment_build['links']);
661 $vars['comment'] = drupal_render($build);