views_handler_field_comment.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_comment.
  5. */
  6. /**
  7. * Field handler to allow linking to a comment.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_comment extends views_handler_field {
  12. /**
  13. * Override init function to provide generic option to link to comment.
  14. */
  15. public function init(&$view, &$options) {
  16. parent::init($view, $options);
  17. if (!empty($this->options['link_to_comment'])) {
  18. $this->additional_fields['cid'] = 'cid';
  19. $this->additional_fields['nid'] = 'nid';
  20. }
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function option_definition() {
  26. $options = parent::option_definition();
  27. $options['link_to_comment'] = array('default' => TRUE, 'bool' => TRUE);
  28. $options['link_to_node'] = array('default' => FALSE, 'bool' => TRUE);
  29. return $options;
  30. }
  31. /**
  32. * Provide link-to-comment option.
  33. */
  34. public function options_form(&$form, &$form_state) {
  35. $form['link_to_comment'] = array(
  36. '#title' => t('Link this field to its comment'),
  37. '#description' => t("Enable to override this field's links."),
  38. '#type' => 'checkbox',
  39. '#default_value' => $this->options['link_to_comment'],
  40. );
  41. $form['link_to_node'] = array(
  42. '#title' => t('Link field to the node if there is no comment.'),
  43. '#type' => 'checkbox',
  44. '#default_value' => $this->options['link_to_node'],
  45. );
  46. parent::options_form($form, $form_state);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function render_link($data, $values) {
  52. if (!empty($this->options['link_to_comment'])) {
  53. $this->options['alter']['make_link'] = TRUE;
  54. $nid = $this->get_value($values, 'nid');
  55. $cid = $this->get_value($values, 'cid');
  56. if (!empty($cid)) {
  57. $this->options['alter']['path'] = "comment/" . $cid;
  58. $this->options['alter']['fragment'] = "comment-" . $cid;
  59. }
  60. // If there is no comment link to the node.
  61. elseif ($this->options['link_to_node']) {
  62. $this->options['alter']['path'] = "node/" . $nid;
  63. }
  64. }
  65. return $data;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function render($values) {
  71. $value = $this->get_value($values);
  72. return $this->render_link($this->sanitize_value($value), $values);
  73. }
  74. }