views_handler_field_comment_link.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /**
  13. * {@inheritdoc}
  14. */
  15. public 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. /**
  22. * {@inheritdoc}
  23. */
  24. public function options_form(&$form, &$form_state) {
  25. $form['text'] = array(
  26. '#type' => 'textfield',
  27. '#title' => t('Text to display'),
  28. '#default_value' => $this->options['text'],
  29. );
  30. $form['link_to_node'] = array(
  31. '#title' => t('Link field to the node if there is no comment.'),
  32. '#type' => 'checkbox',
  33. '#default_value' => $this->options['link_to_node'],
  34. );
  35. parent::options_form($form, $form_state);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function query() {
  41. $this->ensure_my_table();
  42. $this->add_additional_fields();
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function render($values) {
  48. $value = $this->get_value($values, 'cid');
  49. return $this->render_link($this->sanitize_value($value), $values);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function render_link($data, $values) {
  55. $text = !empty($this->options['text']) ? $this->options['text'] : t('view');
  56. $comment = $this->get_value($values);
  57. $nid = $comment->nid;
  58. $cid = $comment->cid;
  59. $this->options['alter']['make_link'] = TRUE;
  60. $this->options['alter']['html'] = TRUE;
  61. if (!empty($cid)) {
  62. $this->options['alter']['path'] = "comment/" . $cid;
  63. $this->options['alter']['fragment'] = "comment-" . $cid;
  64. }
  65. // If there is no comment link to the node.
  66. elseif ($this->options['link_to_node']) {
  67. $this->options['alter']['path'] = "node/" . $nid;
  68. }
  69. return $text;
  70. }
  71. }