views_handler_field_comment.inc

  1. 3.x modules/comment/views_handler_field_comment.inc
  2. 2.x modules/comment/views_handler_field_comment.inc

Definition of views_handler_field_comment.

File

modules/comment/views_handler_field_comment.inc
View source
  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. 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. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['link_to_comment'] = array('default' => TRUE, 'bool' => TRUE);
  25. $options['link_to_node'] = array('default' => FALSE, 'bool' => TRUE);
  26. return $options;
  27. }
  28. /**
  29. * Provide link-to-comment option
  30. */
  31. function options_form(&$form, &$form_state) {
  32. $form['link_to_comment'] = array(
  33. '#title' => t('Link this field to its comment'),
  34. '#description' => t("Enable to override this field's links."),
  35. '#type' => 'checkbox',
  36. '#default_value' => $this->options['link_to_comment'],
  37. );
  38. $form['link_to_node'] = array(
  39. '#title' => t('Link field to the node if there is no comment.'),
  40. '#type' => 'checkbox',
  41. '#default_value' => $this->options['link_to_node'],
  42. );
  43. parent::options_form($form, $form_state);
  44. }
  45. function render_link($data, $values) {
  46. if (!empty($this->options['link_to_comment'])) {
  47. $this->options['alter']['make_link'] = TRUE;
  48. $nid = $this->get_value($values, 'nid');
  49. $cid = $this->get_value($values, 'cid');
  50. if (!empty($cid)) {
  51. $this->options['alter']['path'] = "comment/" . $cid;
  52. $this->options['alter']['fragment'] = "comment-" . $cid;
  53. }
  54. // If there is no comment link to the node.
  55. else if ($this->options['link_to_node']) {
  56. $this->options['alter']['path'] = "node/" . $nid;
  57. }
  58. }
  59. return $data;
  60. }
  61. function render($values) {
  62. $value = $this->get_value($values);
  63. return $this->render_link($this->sanitize_value($value), $values);
  64. }
  65. }