1

I am working on a Drupal 7 site, and I am setting up a template that will be used with the Print module when PDFs are created using that module. The PDFs will draw from pages that list specifications of products, and the PDF itself will act as the printed spec sheet.

I want to completely customize the output of the PDF so that I can choose where each and every field will be placed. Unfortunately, the PDF creation is currently not respecting all of the layout specifics on the website page, so I have decided to customize the print.tpl.php file (part of the Print module) to enact those specific layout elements.

The part I am stuck on is... how do I call in the various individual fields/fieldsets from the content item and have them show up on the template file for printing? For instance, the content type is "Shirts" and one of the fields in question is "Color" ("field_shirtcolor")... how can I access that field/print that field in my custom tpl file?

Thanks!

7
  • 1
    all of those should be in some shape or form in the $node variable, eg $node->field.... a drupal_set_message('<pre>' . print_r($node, TRUE) . '</pre>'); or something similar will tell you what you have to work with.
    – Jimajamma
    Feb 7, 2013 at 19:02
  • @Jimajamma Where would I place that statement? FYI, I tried this as a test and it returned an error message: <?php print render($content['body']); ?> . The error message was " Fatal error: Only variables can be passed by reference in /home/mysite/public_html/sites/all/modules/print/print.tpl.php on line 96" (the code was, indeed, on line 96)
    – hockey2112
    Feb 7, 2013 at 19:04
  • right at the top of your template within some <?php ?> tags
    – Jimajamma
    Feb 7, 2013 at 19:06
  • $content contains all the fields as well, at least inside node templates anyway, eg $content['field_....'] and I don't know why you'd be getting that error from such a print statement.
    – Jimajamma
    Feb 7, 2013 at 19:08
  • And, while I don't know exactly how the print module works, I would suggest instead of modifying the print.tpl.php file in place there, that instead you copy it to your theme and modify it there.
    – Jimajamma
    Feb 7, 2013 at 19:11

1 Answer 1

1

Add to your print.tpl.php code like next for printing certain field:

<?php print render(field_view_field('node', $print['node'], 'field_shirtcolor')); ?>

If field is multiple:

<?php 
  $field = field_get_items('node', $print['node'], 'field_shirtcolor');
  $delta = 0;  // Put here index of multiple field or use `foreach ($field as $val) {}` for scanning multifield values.
  print render(field_view_value('node', $print['node'], 'field_shirtcolor', $field[$delta]));
?>

If field is image:

$image = field_get_items('node', $print['node'], 'field_image');
print render(field_view_value('node', $print['node'], 'field_image', $image[0], array(
  'type' => 'image',
  'settings' => array(
    'image_style' => 'STYLENAME',
    'image_link' => '',
  ),
)));

p.s. Drupal 6 CCK in Drupal 7, 8 called as just Fields.

4
  • I added the <?php print render(field_view_field('node', $print['node'], 'field_shirtcolor')); ?> code to my print.tpl.php file, and it returned this error: Error The website encountered an unexpected error. Please try again later. Any idea on why that would happen?
    – hockey2112
    Feb 8, 2013 at 15:12
  • Please paste next code <?php print_r($print); ?> there: drupalbin.com and provide me link...
    – Nikit
    Feb 8, 2013 at 23:42
  • Maybe I am not understanding... I added <?php print_r($print); ?> to the print.tpl.php file, but it did not have any noticeable effect on that print-ready version of the node.
    – hockey2112
    Feb 9, 2013 at 3:49
  • let's do next: <pre><?php print_r($print);?></pre> in print.tpl.php and drupalbin. Be sure that you have use print.tpl.php in your theme, and adding any changes (any sample text) appear in print version of node...
    – Nikit
    Feb 9, 2013 at 4:04

Your Answer

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

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