views_handler_field_comment_link.inc

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

Definition of views_handler_field_comment_link.

File

modules/comment/views_handler_field_comment_link.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_comment_link.
  5. */
  6. /**
  7. * Base field handler to present a link.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_comment_link extends views_handler_field_entity {
  12. function construct() {
  13. parent::construct();
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['text'] = array('default' => '', 'translatable' => TRUE);
  18. $options['link_to_node'] = array('default' => FALSE, 'bool' => TRUE);
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. $form['text'] = array(
  23. '#type' => 'textfield',
  24. '#title' => t('Text to display'),
  25. '#default_value' => $this->options['text'],
  26. );
  27. $form['link_to_node'] = array(
  28. '#title' => t('Link field to the node if there is no comment.'),
  29. '#type' => 'checkbox',
  30. '#default_value' => $this->options['link_to_node'],
  31. );
  32. parent::options_form($form, $form_state);
  33. }
  34. function query() {
  35. $this->ensure_my_table();
  36. $this->add_additional_fields();
  37. }
  38. function render($values) {
  39. $value = $this->get_value($values, 'cid');
  40. return $this->render_link($this->sanitize_value($value), $values);
  41. }
  42. function render_link($data, $values) {
  43. $text = !empty($this->options['text']) ? $this->options['text'] : t('view');
  44. $comment = $this->get_value($values);
  45. $nid = $comment->nid;
  46. $cid = $comment->cid;
  47. $this->options['alter']['make_link'] = TRUE;
  48. $this->options['alter']['html'] = TRUE;
  49. if (!empty($cid)) {
  50. $this->options['alter']['path'] = "comment/" . $cid;
  51. $this->options['alter']['fragment'] = "comment-" . $cid;
  52. }
  53. // If there is no comment link to the node.
  54. else if ($this->options['link_to_node']) {
  55. $this->options['alter']['path'] = "node/" . $nid;
  56. }
  57. return $text;
  58. }
  59. }