views_handler_field_user_picture.inc

  1. 3.x modules/user/views_handler_field_user_picture.inc
  2. 2.x modules/user/views_handler_field_user_picture.inc

Definition of views_handler_field_user_picture.

File

modules/user/views_handler_field_user_picture.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_user_picture.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows using a themed user link.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_user_picture extends views_handler_field {
  12. function construct() {
  13. parent::construct();
  14. $this->additional_fields['uid'] = 'uid';
  15. $this->additional_fields['name'] = 'name';
  16. $this->additional_fields['mail'] = 'mail';
  17. }
  18. function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
  19. if ($inline) {
  20. return 'span';
  21. }
  22. if ($none_supported) {
  23. if ($this->options['element_type'] === '0') {
  24. return '';
  25. }
  26. }
  27. if ($this->options['element_type']) {
  28. return check_plain($this->options['element_type']);
  29. }
  30. if ($default_empty) {
  31. return '';
  32. }
  33. if (isset($this->definition['element type'])) {
  34. return $this->definition['element type'];
  35. }
  36. return 'div';
  37. }
  38. function option_definition() {
  39. $options = parent::option_definition();
  40. $options['link_photo_to_profile'] = array('default' => TRUE, 'bool' => TRUE);
  41. $options['image_style'] = array('default' => '');
  42. return $options;
  43. }
  44. function options_form(&$form, &$form_state) {
  45. parent::options_form($form, $form_state);
  46. $form['link_photo_to_profile'] = array(
  47. '#title' => t("Link to user's profile"),
  48. '#description' => t("Link the user picture to the user's profile"),
  49. '#type' => 'checkbox',
  50. '#default_value' => $this->options['link_photo_to_profile'],
  51. );
  52. if (module_exists('image')) {
  53. $styles = image_styles();
  54. $style_options = array('' => t('Default'));
  55. foreach ($styles as $style) {
  56. $style_options[$style['name']] = $style['name'];
  57. }
  58. $form['image_style'] = array(
  59. '#title' => t('Image style'),
  60. '#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')))),
  61. '#type' => 'select',
  62. '#options' => $style_options,
  63. '#default_value' => $this->options['image_style'],
  64. );
  65. }
  66. }
  67. function render($values) {
  68. if ($this->options['image_style'] && module_exists('image')) {
  69. // @todo: Switch to always using theme('user_picture') when it starts
  70. // supporting image styles. See http://drupal.org/node/1021564
  71. if ($picture_fid = $this->get_value($values)) {
  72. $picture = file_load($picture_fid);
  73. $picture_filepath = $picture->uri;
  74. }
  75. else {
  76. $picture_filepath = variable_get('user_picture_default', '');
  77. }
  78. if (file_valid_uri($picture_filepath)) {
  79. $output = theme('image_style', array('style_name' => $this->options['image_style'], 'path' => $picture_filepath));
  80. if ($this->options['link_photo_to_profile'] && user_access('access user profiles')) {
  81. $uid = $this->get_value($values, 'uid');
  82. $output = l($output, "user/$uid", array('html' => TRUE));
  83. }
  84. }
  85. else {
  86. $output = '';
  87. }
  88. }
  89. else {
  90. // Fake an account object.
  91. $account = new stdClass();
  92. if ($this->options['link_photo_to_profile']) {
  93. // Prevent template_preprocess_user_picture from adding a link
  94. // by not setting the uid.
  95. $account->uid = $this->get_value($values, 'uid');
  96. }
  97. $account->name = $this->get_value($values, 'name');
  98. $account->mail = $this->get_value($values, 'mail');
  99. $account->picture = $this->get_value($values);
  100. $output = theme('user_picture', array('account' => $account));
  101. }
  102. return $output;
  103. }
  104. }