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