3

I have some fields for nodes. I hide the content and render fields separately as below.

hide($content);
print render($content['field_01']);
print render($content['field_02']);
print render($content['field_03']);
print render($content['field_04']);
print render($content['field_05']);

I set different field formats in content type at Manage Display" like field_01 is an image and i set large image in Full Content and Thumb image in Teaser view mode. field_02 is a taxonomy reference field which i set Link in Full Content and Plain Text in Teaser view mode.

When i render fields in node template, all fields render as view mode "Full". Now i want render all fields from default view mode "Full". Only I want to render field_01 & field_02 from Teaser view mode.

2

Let me give you an example. I have one date field into my content type. Now I want to render that field twice. For getting that that i will print the output as Birthdate : Feb 22, 2006 for first instance and for second I want to print Age : 7 yrs old. In manage display I have format to date as well as Time ago and I want to use both format.

The easiest way to do this would be to create a second variable in your theme_preprocess_node() function, pull out the value that you want to format a second time, format it differently, and then print the variable where you want to see it a second time in your template. Dates are particularly easy to format using the format_date() function, so you don't need to use a different views mode to achieve this.

In your theme's template.php file add something like;

function mytheme_preprocess_node(&$variables) {
  $node = $variables['node']; // Nice shorthand.
  $lang = $node->language; // Shorthand again.
  $variables['new_date'] = format_date($node->field_date[$lang][0], 'medium');
}

Then in your node--whatever.tpl.php file print it out like;

<div class="time-ago"><?php print $new_date; ?></div>
  • Hello Jen, thank you for solution. some modification required in format_date(..) as its gaves an error Warning: date_timezone_set() expects parameter 1 to be DateTime This worked for me $variables['new_date'] = 'Age :' . format_interval((time() - strtotime($node->field_date[$lang][0]['value'])) , 2); – Santosh Mar 3 '14 at 16:24
1

You can write a small module and implement hook_node_view to alter what fields are displayed in another view mode.

If thats too much custom coding modules like Display Suite let you easily create new view_modes or field display formatters with little or no code.

EDIT:

the Template you're refering to renders after the hooks and node loading occurs for the node. Thats why the template just does render($content['field_01']). To change how field_01 looks you have to alter/configure its desired output view prior to hitting the template. Thats what the hook(s) and for instance DS let you do.

1

It is a little hard to tell exactly what you are trying to achieve, but if you only want to print some fields when in the teaser mode you can do something like this:

// Only print fields 1 & 2 for teasers.
if ($view_mode == 'teaser') {
  print render($content['field_01']);
  print render($content['field_02']);
}
// Print these fields always.
print render($content['field_03']);
print render($content['field_04']);
print render($content['field_05']);

It's worth noting though that you could just set the fields to hidden via the display settings UI instead of this.

Or do you mean you want all the fields to always display, but you want them to use your teaser display settings for the first 2 fields even when viewing the full version?

If that is the case, just change the display settings for those fields for the default view mode via the display settings UI. I don't see why you should need any custom code to achieve this.

  • Let me give you an example. I have one date field into my content type. so i want to render that field twice. so that i will print the output as Birthdate : Feb 22, 2006 for first instance and for second i want to print Age : 7 yrs old. in manage display we have format to date as well as Time ago. i want to use both format. – Santosh Jul 29 '13 at 10:26
0

Instead of using render simply use node_view instead:

print node_view($node, $view_mode = 'teaser');

The node should be available in the node template, if using this outside of the node template you will need to get the node object.

So in the above example we are assuming that your teaser view mode shows only field 1 and 2 or whatever fields you want. If not then make a view mode that displays what you want and render it according to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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