5 * Definition of views_handler_field_user_picture.
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_picture extends views_handler_field {
14 function construct() {
16 $this->additional_fields['uid'] = 'uid';
17 $this->additional_fields['name'] = 'name';
18 $this->additional_fields['mail'] = 'mail';
21 function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
25 if ($none_supported) {
26 if ($this->options['element_type'] === '0') {
30 if ($this->options['element_type']) {
31 return check_plain($this->options['element_type']);
36 if (isset($this->definition['element type'])) {
37 return $this->definition['element type'];
43 function option_definition() {
44 $options = parent::option_definition();
45 $options['link_photo_to_profile'] = array('default' => TRUE, 'bool' => TRUE);
46 $options['image_style'] = array('default' => '');
50 function options_form(&$form, &$form_state) {
51 parent::options_form($form, $form_state);
52 $form['link_photo_to_profile'] = array(
53 '#title' => t("Link to user's profile"),
54 '#description' => t("Link the user picture to the user's profile"),
55 '#type' => 'checkbox',
56 '#default_value' => $this->options['link_photo_to_profile'],
59 if (module_exists('image')) {
60 $styles = image_styles();
61 $style_options = array('' => t('Default'));
62 foreach ($styles as $style) {
63 $style_options[$style['name']] = $style['name'];
66 $form['image_style'] = array(
67 '#title' => t('Image style'),
68 '#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')))),
70 '#options' => $style_options,
71 '#default_value' => $this->options['image_style'],
76 function render($values) {
77 if ($this->options['image_style'] && module_exists('image')) {
78 // @todo: Switch to always using theme('user_picture') when it starts
79 // supporting image styles. See http://drupal.org/node/1021564
80 if ($picture_fid = $this->get_value($values)) {
81 $picture = file_load($picture_fid);
82 $picture_filepath = $picture->uri;
85 $picture_filepath = variable_get('user_picture_default', '');
87 if (file_valid_uri($picture_filepath)) {
88 $output = theme('image_style', array('style_name' => $this->options['image_style'], 'path' => $picture_filepath));
89 if ($this->options['link_photo_to_profile'] && user_access('access user profiles')) {
90 $uid = $this->get_value($values, 'uid');
91 $output = l($output, "user/$uid", array('html' => TRUE));
99 // Fake an account object.
100 $account = new stdClass();
101 if ($this->options['link_photo_to_profile']) {
102 // Prevent template_preprocess_user_picture from adding a link
103 // by not setting the uid.
104 $account->uid = $this->get_value($values, 'uid');
106 $account->name = $this->get_value($values, 'name');
107 $account->mail = $this->get_value($values, 'mail');
108 $account->picture = $this->get_value($values);
109 $output = theme('user_picture', array('account' => $account));