1

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;

} }

  • I can't seem to make the code display properly. – Justin Dec 28 '11 at 16:30
  • what theme do you use? Bartik shows the profile image with the blog post. – uwe Dec 28 '11 at 16:50
  • Omega. My previous comment refers to not being able to make the code display here, in the question ;) – Justin Dec 29 '11 at 17:24
  • A very minor adjustment for people who might use this code. When setting class attributes, you should use an array. So: 'attributes' => array('class' => array('author-image')),. This way you can easily add standard classes within your theme. – mbrakken Jul 14 '13 at 16:48
1

In your code at

 $node->field_blog_image['und'][0]

this is still an array so you need to access the value of the field one more level deep in to that array you can access it as follows.

$node->field_blog_image['und'][0]['safe_value']

Rest of code looks good to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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