Projet

Général

Profil

Paste
Télécharger (3,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / user / views_handler_field_user_name.inc @ 5d12d676

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
   * Add uid in the query so we can test for anonymous if needed.
17
   */
18
  public function init(&$view, &$data) {
19
    parent::init($view, $data);
20
    if (!empty($this->options['overwrite_anonymous']) || !empty($this->options['format_username'])) {
21
      $this->additional_fields['uid'] = 'uid';
22
    }
23
  }
24

    
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function option_definition() {
29
    $options = parent::option_definition();
30

    
31
    $options['overwrite_anonymous'] = array('default' => FALSE, 'bool' => TRUE);
32
    $options['anonymous_text'] = array('default' => '', 'translatable' => TRUE);
33
    $options['format_username'] = array('default' => TRUE, 'bool' => TRUE);
34

    
35
    return $options;
36
  }
37

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

    
66
    parent::options_form($form, $form_state);
67
  }
68

    
69
  /**
70
   * {@inheritdoc}
71
   */
72
  public function render_link($data, $values) {
73
    $account = new stdClass();
74
    $account->uid = $this->get_value($values, 'uid');
75
    $account->name = $this->get_value($values);
76
    if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
77
      if (!empty($this->options['overwrite_anonymous']) && !$account->uid) {
78
        // This is an anonymous user, and we're overriting the text.
79
        return check_plain($this->options['anonymous_text']);
80
      }
81
      elseif (!empty($this->options['link_to_user'])) {
82
        $account->name = $this->get_value($values);
83
        return theme('username', array('account' => $account));
84
      }
85
    }
86
    // If we want a formatted username, do that.
87
    if (!empty($this->options['format_username']) && !is_null($account->uid)) {
88
      return format_username($account);
89
    }
90
    // Otherwise, there's no special handling, so return the data directly.
91
    return $data;
92
  }
93

    
94
}