1

I have used this code to print normal text fields in a print.tpl.php template file:

<?php if(isset($node->field_mytextfield['und'][0]['value'])) { 
          print $node->field_mytextfield['und'][0]['value'];
            }
     ?>

But that same code will not work with Computed Fields I have set up in that same content type. The content type has a "Print" display type, and I have tried all of the Format options (Unsanitized, Plain Text, Filtered Markup, and Raw Value No Display Code) for the computed fields, to no avail. How can I print the formatted values of those computed fields, exactly as they are displayed by the actual fields themselves?

I also have a Textarea field that prints as a single line of unformatted text using the code above. How can I print the formatted value of that field when Full HTML is used to enter info into that field?

Thanks!

1 Answer 1

2

The Computed field defines its own formatting in the Display Code (PHP) box. If you have done your formatting there, when the page is processed, $vars will contain the output as:

  1. Unsanitized: the output of Display Code (PHP) ($display_output) is unchanged.

  2. Plain text: $display_output is filtered through the Plain Text filter.

  3. Filtered markup: Non-permitted HTML tags are stripped from $display_output.

  4. Raw Value: Display Code (PHP) is ignored and the value from Computed Code (PHP) is used ($entity_field[0]['value'] ).

This is fine for a rendered field using $vars. The Display output code is used when the node is preprocessed. If, however you wish to access the fields from the $node object, only the field value (Raw value) is presented. So you either have to duplicate the Display output code in your template, or you have to move the formatting into the Computed code box.

If you set your $entity_field[0]['value'] to the actual formatted string, then use the default $display_output = $entity_field_item['value']; in the Display output, you should be able to use the above code to display the field as is.

This is why it is much easier to use the processed fields. Instead of coding something like:

print $node->field_fieldname[LANGUAGE_NONE][0]['value'];

If $vars['field_fieldname'] has been set, the template file receives the result of extract($vars). Each array element becomes a variable in its own right and all you have to do in the template file is:

print $field_fieldname;

or

print render($content['field_fieldname'];

if you have not separately included it in $vars in a preprocress. Much simpler when you understand how the data gets moved around.

Your Answer

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

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