2

I'm trying to print a user profile's picture in a block that gets displayed on a node page. I have the nodes author be loaded as in the following code:

<?php 
$node = menu_get_object('node');
$user = user_load($node->uid);
?>

This is the code I have come up with for the block:

<div class="author-block">
  <h4>Meet the author</h4>
  <?php print drupal_render(field_view_field('user', $user, 'picture', 'user-pic-style'));?>
</div>

This isn't working and doesn't print anything. I want the user's picture be loaded if users have one, or the default user picture if they don't. I have other custom user fields that are printing just fine using the field_view_field(), so I am wondering why I am not having luck.

3
  • May I suggest that you re-title the question to be less specific to a particular function? Just "How do I programmatically render a picture", perhaps?
    – Letharion
    Jan 7, 2012 at 11:23
  • If you have the image url, you could do this - print theme('image_style', array('style_name' => 'user-pic-style', 'path' => $user_picture_path));
    – SGhosh
    Jan 31, 2013 at 9:40
  • 1
    Please don't use $user = user_load(... it can conflict with global $user. Use $account = ... instead. Jan 31, 2013 at 9:44

2 Answers 2

2

Try this

<div class="author-block">
  <h4>Meet the author</h4>
  <?php print drupal_render(field_view_field('user', $user, 'picture', array('settings' => array('image_style' => 'user-pic-style'))));?>
</div>
3
0

I'm not entirely sure why the field isn't returning any data, but I think it has something to do with it being an image, and you not setting a style. Here's how I've done similar things with images before. Please note that I have altered the code below both to protect my original customer, and to hide the fact this was done as a Ctools plugin, instead of a block. So there could be minor issues like misspellings and the like. My apologies if that's the case.

// Create a shortcut to the image we want to render
$image_data = $image_node->field_images[LANGUAGE_NONE][0];

// Image rendering data
$variables = array(
  'style_name' => 'blurb_208x208',
  'path' => $image_data['filename'],
  'alt' => '',
  'title' => '',
);

$styles = image_styles(); // Get all available styles
$style = $styles['blurb_208x208'];

// We are responsible ourselves for ensuring the derived image is available.
$derivative_uri = image_style_path($style['name'], $path_to_original_image);
if (!file_exists($derivative_uri)) {
  image_style_create_derivative(
    $style,
    $image_data['uri'],
    $derivative_uri
  );
}

$markup = '<div class="image-wrapper">' . theme('image_style', $variables) . '</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.