views_handler_field_comment_username.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_comment_username.
  5. */
  6. /**
  7. * Field handler to allow linking to a user account or homepage.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_comment_username extends views_handler_field {
  12. /**
  13. * Override init function to add uid and homepage fields.
  14. */
  15. public function init(&$view, &$data) {
  16. parent::init($view, $data);
  17. $this->additional_fields['uid'] = 'uid';
  18. $this->additional_fields['homepage'] = 'homepage';
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function option_definition() {
  24. $options = parent::option_definition();
  25. $options['link_to_user'] = array('default' => TRUE, 'bool' => TRUE);
  26. return $options;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function options_form(&$form, &$form_state) {
  32. $form['link_to_user'] = array(
  33. '#title' => t("Link this field to its user or an author's homepage"),
  34. '#type' => 'checkbox',
  35. '#default_value' => $this->options['link_to_user'],
  36. );
  37. parent::options_form($form, $form_state);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function render_link($data, $values) {
  43. if (!empty($this->options['link_to_user'])) {
  44. $account = new stdClass();
  45. $account->uid = $this->get_value($values, 'uid');
  46. $account->name = $this->get_value($values);
  47. $account->homepage = $this->get_value($values, 'homepage');
  48. return theme('username', array(
  49. 'account' => $account
  50. ));
  51. }
  52. else {
  53. return $data;
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function render($values) {
  60. $value = $this->get_value($values);
  61. return $this->render_link($this->sanitize_value($value), $values);
  62. }
  63. }