Projet

Général

Profil

Paste
Télécharger (2,83 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / user / views_handler_field_user_name.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_user_name.
6
 */
7

    
8
/**
9
 * Field handler for a simple renderer that allows using a themed user link.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_user_name extends views_handler_field_user {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['overwrite_anonymous'] = array('default' => FALSE, 'bool' => TRUE);
22
    $options['anonymous_text'] = array('default' => '', 'translatable' => TRUE);
23
    $options['format_username'] = array('default' => TRUE, 'bool' => TRUE);
24

    
25
    return $options;
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form(&$form, &$form_state) {
32
    $form['format_username'] = array(
33
      '#title' => t('Use formatted username'),
34
      '#type' => 'checkbox',
35
      '#default_value' => !empty($this->options['format_username']),
36
      '#description' => t('If checked, the username will be formatted by the system. If unchecked, it will be displayed raw.'),
37
      '#fieldset' => 'more',
38
    );
39
    $form['overwrite_anonymous'] = array(
40
      '#title' => t('Overwrite the value to display for anonymous users'),
41
      '#type' => 'checkbox',
42
      '#default_value' => !empty($this->options['overwrite_anonymous']),
43
      '#description' => t('Enable to display different text for anonymous users.'),
44
      '#fieldset' => 'more',
45
    );
46
    $form['anonymous_text'] = array(
47
      '#title' => t('Text to display for anonymous users'),
48
      '#type' => 'textfield',
49
      '#default_value' => $this->options['anonymous_text'],
50
      '#dependency' => array(
51
        'edit-options-overwrite-anonymous' => array(1),
52
      ),
53
      '#fieldset' => 'more',
54
    );
55

    
56
    parent::options_form($form, $form_state);
57
  }
58

    
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public 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 we don't have a UID, we can't format anything.
67
    if (!isset($account->uid)) {
68
      return $data;
69
    }
70
    if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
71
      if (!empty($this->options['overwrite_anonymous']) && !$account->uid) {
72
        // This is an anonymous user, and we're overriting the text.
73
        return check_plain($this->options['anonymous_text']);
74
      }
75
      elseif (!empty($this->options['link_to_user'])) {
76
        $account->name = $this->get_value($values);
77
        return theme('username', array('account' => $account));
78
      }
79
    }
80
    // If we want a formatted username, do that.
81
    if (!empty($this->options['format_username']) && !is_null($account->uid)) {
82
      return format_username($account);
83
    }
84
    // Otherwise, there's no special handling, so return the data directly.
85
    return $data;
86
  }
87

    
88
}