4

I'm using the Printer, email and PDF versions module to render a custom print page in Drupal 7. I'd like to theme the fields individually and have worked that out to the point of using nasty php in my custom template itself.

So, I have a content type called ecard and following the module's documentation, I'm using print--node--ecard.tpl.php (based on the modules's core print.tpl.php file) for my custom print template. That works fine but the usual ways of rendering individual fields like in a node template don't.

I can do this within print--node--ecard.tpl.php to render an indivdual field called field_ecard_logo:

<?php 
$ecard_logo = field_view_value('node', $node, 'field_ecard_logo', $node->field_ecard_logo[$node->language][0]);
print render($ecard_logo);
?>

... works but kind of nasty. I'd rather put all the logic in my theme's template.php and simply print a variable I've created. Looking at what preprocess functions the Print module uses, I see preprocess_print and preprocess_node. So I tried this in template.php:

  function MYTHEME_preprocess_print($vars) {
      $node = $vars['node'];
      $vars['ecard_logo'] = field_view_value('node', $node, 'field_ecard_logo', $node->field_ecard_logo[$node->language][0]);
    }

and then in print--node--ecard.tpl.php, I simply do:

<?php print $ecard_logo; ?>

but that does not work. I tried different incarnations of all this but no joy so at this point I'm stumped.

  • You should pass $vars variable by reference.. – xurshid29 Sep 12 '13 at 6:21
7

After a few days of working on this, it ended up being pretty trivial to accomplish this so I'll answer my own question for those who need help in the future. I had most of the solution working but in my custom print template, the key was to have:

<?php print render($ecard_logo); ?>

NOT:

<?php print $ecard_logo; ?> 

So the full function in template.php is:

/**
  * Renders the content of the print page using the theme api.
  */
function usfig_preprocess_print(&$vars, $hook) {

// Global node.
$node = $vars['node'];
// Compartmentalize only for ecard content type.
if($vars['node']->type == 'ecard') {

// Create a var and render using field_view_value, we don't need anything special for this, otherwise use field_get_items for data parts.
$vars['ecard_logo'] = field_view_value('node', $node, 'field_ecard_logo', $node->field_ecard_logo[$node->language][0]);
}

}

... and then in my custom print template I do:

 <?php print render($ecard_logo); ?>

and it works!

Extra credit:

You can create and render an image style with field_view_value as well like so in your theme's template.php file:

 $vars['ecard_image'] = field_view_value('node', $node, 'field_my_image', $node->field_my_image[$node->language][0], array(
      'type' => 'image',
      'settings' => array(
      'image_style' => 'my_custom_image_style', 
      'image_link' => 'none',
      ),
      ));

Then just render the variable in your custom template file! What's the advantage to all this? It allows for ultra minimalist markup and precise themeing where layout and exactness are crucial. It cuts way down on the "divitis" that drupal tends to have.

0

From personal experience in these situations it's better to incorporate all the logic into a custom module, so basically you create there your functions for processing data, and these functions are available throughout the whole site by calling the function.

In your current code i think that the variable can be accessed by defining it in your tpl through GLOBAL $ecard_logo; . I haven't tested this so let me know if this helps.

Cheers

protected by kiamlaluno Jul 28 '16 at 9:39

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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