Hi there

I hope this is the right category to place the question ...

I have a template which handles Ajax responses for content types which contain a body field with summary. Using this

$node = node_load($input);
$prerendered_node = node_view( $node, 'default' );

I get everything about the node but when I render the body

render($prerendered_node['body']);

the summary isn't displayed. How can I access the summary of body field?

I tried

field_view_field('node', $node, 'summary');
field_view_field('node', $node, 'body', 'teaser');

but it seems I don't know how to use this function. Is there a better way than this?

$node->body['en'][0]['summary'];

Update
Seems that $node->body['en'][0]['summary']; won't even work. It displays nothing, although var_dump($node->body['en'][0]) shows the information.

Thanks.

Comments

vlad-ghita’s picture

Check the answer here.

yookoala’s picture

Instead of using the body array directly, you should use render() and field_view_field(). When used properly, they would display exactly the same as native rendered output.

You may take a look at these examples:


/*
 * Example 1)
 * standard output of node body
 */
print render(field_view_field('node', $node, 'body')); 

/*
 * Example 2)
 * output as "summary or trimmed"
 * the same as the standard teaser body output
 */
print render(field_view_field('node', $node, 'body', array(
  'type' => 'text_summary_or_trimmed', 
)));

/*
 * Example 3)
 * more control over the output, include
 * 1. display 'text summary or trimmed' of the body
 * 2. hide the label 'Body' from output
 * 3. if there is no summary, trim the text to 150 characters
 *     (instead of 600 by default)
 */
print render(field_view_field('node', $node, 'body', array(
  'label'=>'hidden', 
  'type' => 'text_summary_or_trimmed', 
  'settings'=>array('trim_length' => 150),
))); 
  
   

For more information, please take a look at these:

jmix’s picture

hi, i'm very interested on this, but i can't manage to make it work in my node.tpl.php
in fact i need this to render two "types" of the same field in a same node (one is 'default' and the other 'text_summary_or_trimmed')
but i guess there's something i don't understand as this breaks all my node:

 <?php 
    print render(field_view_field('node', $node, 'field_my_textfield', array(
    	'label'=>'hidden', 
    	'type' => 'text_summary_or_trimmed', 
    	'settings'=>array('trim_length' => 10),
    	)));
    ?>

thanks for helping !

jmix’s picture

oups, in fact it works perfectly in a node.tpl.php, my problem is that i was trying to do this within a field-collection-item.tpl.php...

so it's another issue, but if you have a solution for this...

grigoriunicolae’s picture

in the node--my-content-type.tpl.php file the body variable is accessible. So, you can use something like this:

<?php print render($body[0]['summary']); ?>
debasisnaskar’s picture

Use the below code to print summary in your custom page:

<?php echo $node->body['und'][0]['summary']; ?>