Problem/Motivation

Need to include an image field in forward.tpl.php separate from the $node output... in forward.tpl.php, we have $title, $submitted, $node, and $link. I am using the "Forward Display" when managing my content type and I can include various fields here, but I need to output an image field elsewhere in forward.tpl.php

Proposed resolution

I've taken a stab at writing out the PHP and it comes close, and is probably ugly:

<?php
$path = drupal_get_normal_path($_GET["path"]);
$path = explode("/", $path);
$nid = $path[1];
$forwardthumb = node_load($nid);
$img_uri = $forwardthumb->field_thumb['und'][0]['uri'];
$fid = $forwardthumb->field_thumb['und'][0]['fid'];
$thumbfilename = file_load($fid)->filename;
$derivative_uri = image_style_path($style, $image_uri);
$success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
$new_image_url  = file_create_url($derivative_uri);
?>

The first few lines were conceptualized from this post:
https://drupal.org/node/2078479

I switched things from user to node, uid to nid, etc... but it's not fully working. In my testing, I've gotten the following text to output in my forwarded-email:
http://mysite.com/sites/default/files/styles/blog_thumb/public

(from the $new_image_url variable)

Which tells me that I've got it *partially* working, as it was able to indicate the correct folder / location... but I was never able to actually get it to spit out my image-file-name (e.g. thumbnail.png), just the path to the folder containing my image. Please let me know what I'm doing wrong?

Seems like part of this is a bit wonky, as it seems the $node variable in the template isn't the same $node variable used elsewhere in Drupal...?

I also wanted to share some node.tpl code that works for this, but it doesn't in forward.tpl:

<?php
$path = drupal_get_normal_path($_GET["path"]);
$path = explode("/", $path);
$nid = $path[1];
$forwardthumb = node_load($nid);
?>

And:
<img src="<?php print file_create_url($forwardthumb->field_main_blog_image['und'][0]['uri']); ?>" />

No luck... please help! Thanks!

Comments

nate covington’s picture

Solved the problem, here is the code that I wound up using.

In forward.tpl.php:

<img src="<?php print $customthumbimageurl; ?>" />

Then, in forward.module, after this line:

$logo = (variable_get('forward_header_image', '') == '') ? theme_get_setting('logo') : variable_get('forward_header_image', '');

Add this:

if ($content->field_forward_thumb) {
  $img_name = $content->field_forward_thumb['und'][0]['filename'];
  $img_name = "forward/" . $img_name;
  $style_name = 'forward_thumb';
  $imagePath = image_style_url($style_name, $img_name);
}

And, after:

'link' => ($emailtype == 'email') ? l(t('Read more'), 'forward/emailref', array('absolute' => TRUE, 'query' => array('path' => $returnurl . $form_state['values']['path_cid']))) : FALSE,

Add this:

'customthumbimageurl' => ($emailtype == 'email') ? $imagePath : FALSE,
nate covington’s picture

Issue summary: View changes

included node.tpl sample code that works on the actual site, but doesn't in forward.tpl email template

john.oltman’s picture

Status: Active » Closed (works as designed)

The proper way to do this would be to write a hook_preprocess_forward function and inject new variables - then use them in your own custom forward.tpl.php