Requirements: For nodes of a certain type (blog), I need to show the author information with the author image using a specific Image Style, the user name, the user profile description, and another custom user field.
The method I'm currently using is implementing theme_preprocess_node() to create a variable to pass to my node--blog.tpl.php file that includes the elements I need themed. This means I have calls to theme_image_style() in my theme_preprocess_node() function.
Is this the best method, or is there a "more Drupal" approach I'm missing?
If it helps, here is a snippet of code:
/**
* Hook theme_preprocess_node
*/
function hotness_preprocess_node(&$vars) {
// for blog posts, style date and show intro image
$node = $vars['node'];
if($node->type == 'blog') {
// nice date
$vars['date'] = format_date($node->created, 'custom', 'F j, Y');
// use image style for blog image
$vars['blog_image'] = NULL;
$blog_image = isset($node->field_blog_image['und'][0]) ? $node->field_blog_image['und'][0] : FALSE;
if($blog_image) {
$vars['blog_image'] = theme_image_style(array(
'style_name' => 'introduction-image',
'path' => $blog_image['uri'],
'width' => $blog_image['width'],
'height' => $blog_image['height'],
'alt' => $blog_image['title'], // to make it easier to the end user I just show title
'title' => $blog_image['title'],
'attributes' => array('class' => 'blog-image')
));
}
// I use user_load() because the node object doesn't have the user description.
$author = user_load($node->uid);
$author_image = '';
if(isset($author->picture->uri) && !empty($author->picture->uri)) {
$author_image = theme_image_style(array(
'style_name' => 'blog-author-image',
'path' => $node->picture->uri,
'width' => NULL,
'height' => NULL,
'alt' => t('User .') . $node->name,
'title' => t('User ') . $node->name,
'attributes' => array('class' => array('author-image')),
));
}
$author_name = l($node->name, 'user/' . $node->uid);
$author_desc = isset($author->field_description['und'][0]['safe_value']) ?
'<div class="author-description">' . $author->field_description['und'][0]['safe_value'] . '</div>': '';
// put it all together
$vars['author_block'] = $author_image . $author_name . $author_desc;
} }
'attributes' => array('class' => array('author-image')),
. This way you can easily add standard classes within your theme. – mbrakken Jul 14 '13 at 16:48