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

Definition of views_handler_field_user_name.

File

modules/user/views_handler_field_user_name.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_user_name.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows using a themed user link.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_user_name extends views_handler_field_user {
  12. /**
  13. * Add uid in the query so we can test for anonymous if needed.
  14. */
  15. function init(&$view, &$data) {
  16. parent::init($view, $data);
  17. if (!empty($this->options['overwrite_anonymous']) || !empty($this->options['format_username'])) {
  18. $this->additional_fields['uid'] = 'uid';
  19. }
  20. }
  21. function option_definition() {
  22. $options = parent::option_definition();
  23. $options['overwrite_anonymous'] = array('default' => FALSE, 'bool' => TRUE);
  24. $options['anonymous_text'] = array('default' => '', 'translatable' => TRUE);
  25. $options['format_username'] = array('default' => TRUE, 'bool' => TRUE);
  26. return $options;
  27. }
  28. function options_form(&$form, &$form_state) {
  29. $form['format_username'] = array(
  30. '#title' => t('Use formatted username'),
  31. '#type' => 'checkbox',
  32. '#default_value' => !empty($this->options['format_username']),
  33. '#description' => t('If checked, the username will be formatted by the system. If unchecked, it will be displayed raw.'),
  34. '#fieldset' => 'more',
  35. );
  36. $form['overwrite_anonymous'] = array(
  37. '#title' => t('Overwrite the value to display for anonymous users'),
  38. '#type' => 'checkbox',
  39. '#default_value' => !empty($this->options['overwrite_anonymous']),
  40. '#description' => t('Enable to display different text for anonymous users.'),
  41. '#fieldset' => 'more',
  42. );
  43. $form['anonymous_text'] = array(
  44. '#title' => t('Text to display for anonymous users'),
  45. '#type' => 'textfield',
  46. '#default_value' => $this->options['anonymous_text'],
  47. '#dependency' => array(
  48. 'edit-options-overwrite-anonymous' => array(1),
  49. ),
  50. '#fieldset' => 'more',
  51. );
  52. parent::options_form($form, $form_state);
  53. }
  54. function render_link($data, $values) {
  55. $account = new stdClass();
  56. $account->uid = $this->get_value($values, 'uid');
  57. $account->name = $this->get_value($values);
  58. if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
  59. if (!empty($this->options['overwrite_anonymous']) && !$account->uid) {
  60. // This is an anonymous user, and we're overriting the text.
  61. return check_plain($this->options['anonymous_text']);
  62. }
  63. elseif (!empty($this->options['link_to_user'])) {
  64. $account->name = $this->get_value($values);
  65. return theme('username', array('account' => $account));
  66. }
  67. }
  68. // If we want a formatted username, do that.
  69. if (!empty($this->options['format_username'])) {
  70. return format_username($account);
  71. }
  72. // Otherwise, there's no special handling, so return the data directly.
  73. return $data;
  74. }
  75. }