views_handler_field_comment_node_link.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_comment_node_link.
  5. */
  6. /**
  7. * Handler for showing comment module's node link.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_comment_node_link extends views_handler_field_entity {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function construct() {
  16. parent::construct();
  17. // Add the node fields that comment_link will need.
  18. $this->additional_fields['nid'] = array(
  19. 'field' => 'nid',
  20. );
  21. $this->additional_fields['type'] = array(
  22. 'field' => 'type',
  23. );
  24. $this->additional_fields['comment'] = array(
  25. 'field' => 'comment',
  26. );
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function option_definition() {
  32. $options = parent::option_definition();
  33. $options['teaser'] = array('default' => FALSE, 'bool' => TRUE);
  34. return $options;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function options_form(&$form, &$form_state) {
  40. $form['teaser'] = array(
  41. '#type' => 'checkbox',
  42. '#title' => t('Show teaser-style link'),
  43. '#default_value' => $this->options['teaser'],
  44. '#description' => t('Show the comment link in the form used on standard node teasers, rather than the full node form.'),
  45. );
  46. parent::options_form($form, $form_state);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function query() {
  52. $this->ensure_my_table();
  53. $this->add_additional_fields();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function render($values) {
  59. // Build fake $node.
  60. $node = $this->get_value($values);
  61. // Call comment.module's hook_link:
  62. // comment_link($type, $node = NULL, $teaser = FALSE)
  63. // Call node by reference so that something is changed here.
  64. comment_node_view($node, $this->options['teaser'] ? 'teaser' : 'full');
  65. // Question: should we run these through:
  66. // drupal_alter('link', $links, $node);
  67. // Might this have unexpected consequences if these hooks expect items in
  68. // $node that we don't have?
  69. // Only render the links, if they are defined.
  70. return !empty($node->content['links']['comment']) ? drupal_render($node->content['links']['comment']) : '';
  71. }
  72. }