Project

General

Profile

Paste
Download (4.21 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / modules / user / views_handler_field_user_picture.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_user_picture.
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_picture extends views_handler_field {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function construct() {
19
    parent::construct();
20
    $this->additional_fields['uid'] = 'uid';
21
    $this->additional_fields['name'] = 'name';
22
    $this->additional_fields['mail'] = 'mail';
23
  }
24

    
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
29
    if ($inline) {
30
      return 'span';
31
    }
32
    if ($none_supported) {
33
      if ($this->options['element_type'] === '0') {
34
        return '';
35
      }
36
    }
37
    if ($this->options['element_type']) {
38
      return check_plain($this->options['element_type']);
39
    }
40
    if ($default_empty) {
41
      return '';
42
    }
43
    if (isset($this->definition['element type'])) {
44
      return $this->definition['element type'];
45
    }
46

    
47
    return 'div';
48
  }
49

    
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function option_definition() {
54
    $options = parent::option_definition();
55
    $options['link_photo_to_profile'] = array('default' => TRUE, 'bool' => TRUE);
56
    $options['image_style'] = array('default' => '');
57
    return $options;
58
  }
59

    
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function options_form(&$form, &$form_state) {
64
    parent::options_form($form, $form_state);
65
    $form['link_photo_to_profile'] = array(
66
      '#title' => t("Link to user's profile"),
67
      '#description' => t("Link the user picture to the user's profile"),
68
      '#type' => 'checkbox',
69
      '#default_value' => $this->options['link_photo_to_profile'],
70
    );
71

    
72
    if (module_exists('image')) {
73
      $styles = image_styles();
74
      $style_options = array('' => t('Default'));
75
      foreach ($styles as $style) {
76
        $style_options[$style['name']] = $style['name'];
77
      }
78

    
79
      $form['image_style'] = array(
80
        '#title' => t('Image style'),
81
        '#description' => t('Using <em>Default</em> will use the site-wide image style for user pictures set in the <a href="!account-settings">Account settings</a>.', array('!account-settings' => url('admin/config/people/accounts', array('fragment' => 'edit-personalization')))),
82
        '#type' => 'select',
83
        '#options' => $style_options,
84
        '#default_value' => $this->options['image_style'],
85
      );
86
    }
87
  }
88

    
89
  /**
90
   * {@inheritdoc}
91
   */
92
  public function render($values) {
93
    if ($this->options['image_style'] && module_exists('image')) {
94
      // @todo Switch to always using theme('user_picture') when it starts
95
      // supporting image styles. See http://drupal.org/node/1021564
96
      if ($picture_fid = $this->get_value($values)) {
97
        $picture = file_load($picture_fid);
98
        if (!empty($picture)) {
99
          $picture_filepath = $picture->uri;
100
        }
101
        else {
102
          $picture_filepath = variable_get('user_picture_default', '');
103
        }
104
      }
105
      else {
106
        $picture_filepath = variable_get('user_picture_default', '');
107
      }
108
      if (file_valid_uri($picture_filepath)) {
109
        $account = user_load($this->get_value($values, 'uid'));
110
        $alt = t("@user's picture", array('@user' => format_username($account)));
111
        $output = theme('image_style', array('style_name' => $this->options['image_style'], 'path' => $picture_filepath, 'alt' => $alt));
112
        if ($this->options['link_photo_to_profile'] && user_access('access user profiles')) {
113
          $uid = $this->get_value($values, 'uid');
114
          $output = l($output, "user/$uid", array('html' => TRUE));
115
        }
116
      }
117
      else {
118
        $output = '';
119
      }
120
    }
121
    else {
122
      // Fake an account object.
123
      $account = new stdClass();
124
      if ($this->options['link_photo_to_profile']) {
125
        // Prevent template_preprocess_user_picture from adding a link
126
        // by not setting the uid.
127
        $account->uid = $this->get_value($values, 'uid');
128
      }
129
      $account->name = $this->get_value($values, 'name');
130
      $account->mail = $this->get_value($values, 'mail');
131
      $account->picture = $this->get_value($values);
132
      $output = theme('user_picture', array('account' => $account));
133
    }
134

    
135
    return $output;
136
  }
137

    
138
}