views_handler_field_user_name.inc

  1. 3.x modules/user/views_handler_field_user_name.inc
  2. 2.x modules/user/views_handler_field_user_name.inc

File

modules/user/views_handler_field_user_name.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide simple renderer that allows using a themed user link
  4. */
  5. class views_handler_field_user_name extends views_handler_field_user {
  6. /**
  7. * Add uid in the query so we can test for anonymous if needed.
  8. */
  9. function init(&$view, &$data) {
  10. parent::init($view, $data);
  11. if (!empty($this->options['overwrite_anonymous'])) {
  12. $this->additional_fields['uid'] = 'uid';
  13. }
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['overwrite_anonymous'] = array('default' => FALSE);
  18. $options['anonymous_text'] = array('default' => '', 'translatable' => TRUE);
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $form['overwrite_anonymous'] = array(
  24. '#title' => t('Overwrite the value to display for anonymous users'),
  25. '#type' => 'checkbox',
  26. '#default_value' => !empty($this->options['overwrite_anonymous']),
  27. '#description' => t('If selected, you will see a field to enter the text to use for anonymous users.'),
  28. );
  29. $form['anonymous_text'] = array(
  30. '#title' => t('Text to display for anonymous users'),
  31. '#type' => 'textfield',
  32. '#default_value' => $this->options['anonymous_text'],
  33. '#process' => array('views_process_dependency'),
  34. '#dependency' => array(
  35. 'edit-options-overwrite-anonymous' => array(1),
  36. ),
  37. );
  38. }
  39. function render_link($data, $values) {
  40. if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
  41. $account = new stdClass();
  42. $account->uid = $values->{$this->aliases['uid']};
  43. if (!empty($this->options['overwrite_anonymous']) && !$account->uid) {
  44. // This is an anonymous user, and we're overriting the text.
  45. return check_plain($this->options['anonymous_text']);
  46. }
  47. elseif (!empty($this->options['link_to_user'])) {
  48. $account->name = $values->{$this->field_alias};
  49. return theme('username', $account);
  50. }
  51. }
  52. // Otherwise, there's no special handling, so return the data directly.
  53. return $data;
  54. }
  55. }