1

I am using print module. I am also using devel module. In print.tpl.php file, I see a php code that prints the content as print $content. I don't want to print the entire content. I think content is an array. How can I print manually some fields from the content variable using the devel module or in php?

Thanks. D


    1.

print render($content['field_machine_name']);

Get this error: Fatal error: Only variables can be passed by reference in /..sites/all/modules/print/print.tpl.php on line 100

  1. php print $content['field_machine_name'];

I get left error only (<)

    3.

print $fields['field_vehicle_options']->content;

I get completely blank


I am using bartik 7.22.

  1. themes > bartik > template.php

Added the below function.

function bartik_preprocess_print(&$vars, $hook) {

// Global node.
$node = $vars['node'];

// Create a var and render using field_view_value
$vars['test'] = field_view_value('node', $node, 'field_price', $node->field_price[$node->language][0]);

}
  1. themes > bartik > template > print.tpl.php

I added: print render($test);

  1. I went to my page http://bettylist.com/ac/node/12 > clicked printer friendly link > I did not see the value of variable.

In print.tpl.php, what works:

print $content; //I GET CONTENT IN PRINT PAGE
print dsm($title); // I GET VALUE OF TITLE IN PRINT PAGE

Please advise. Thanks, D


It worked. I have added one more variable as below:

function bartik_preprocess_print(&$vars, $hook) {

// Global node.
$node = $vars['node'];

// Create a var and render using field_view_value
$vars['price'] = field_view_value('node', $node, 'field_price', $node->field_price[$node->language][0]);


// Create a var and render using field_view_value
$vars['vehicleOption1'] = field_view_value('node', $node, 'field_vehicle_option_1', $node->field_vehicle_option_1[$node->language][0]);

}

In print.tpl.php, I added

print render($test);
print render($vehicleOption1);

But I am only getting the value of the last variable $vars'vehicleOption1'.

Please advise.

4
  • It worked after I cleared the cache. Thank you so so much. You are great. Now, if I want 10 variables to print, I have to do the same for the 10 variables. That mean, I have to declare 10 variables in template and then print it in print.tpl.php? Thanks again.
    – autop
    Commented Apr 1, 2015 at 14:59
  • yes, you need to declare all variables you want in template.php and than use them in print.tpl.php file.
    – mixerowsky
    Commented Apr 1, 2015 at 16:51
  • I think I am not sure how to declare. Can you please see my above message and see what I did wrong?
    – autop
    Commented Apr 1, 2015 at 20:04
  • In your update above you declared two variables in template.php file, price and vehicleOption1, but you didn't declare variable test so you couldn't use it (as you did in your print.tpl.php file).
    – mixerowsky
    Commented Apr 3, 2015 at 10:44

1 Answer 1

1

Try with this:

<?php print render($content['your_field_name']); ?>

This should print the field you want.

Change 'your_field_name' with machine name of the desired field.

You can install and enable Devel module and add this:

<?php dsm($node); ?>
<?php dsm($content); ?>

in your print.tpl.php file. You will see all $node and content fields.

UPDATE

Also, you can add this function in your template.php file:

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

// Global node.
$node = $vars['node'];

// Create a var and render using field_view_value
$vars['any_variable'] = field_view_value('node', $node, 'your_field_name', $node->your_field_name[$node->language][0]);

}

Replace THEME with your theme name, any_variable with any name you want for this variable and your_field_name with machine name of your field.

After this you can render your field in print.tpl.php file like this:

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

Again, change any_variable with the name you choose for this variable in function from template.php file.

2
  • 1. <?php print render($content['field_machine_name']); ?> Get this error: Fatal error: Only variables can be passed by reference in /..sites/all/modules/print/print.tpl.php on line 100 2. <?php print $content['field_machine_name']; ?> I get left error only (<) 3. <?php print $fields['field_vehicle_options']->content; ?> I get completely blank.
    – autop
    Commented Mar 31, 2015 at 22:05
  • @Drupol see update in my answer...
    – mixerowsky
    Commented Mar 31, 2015 at 22:32

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