4

I have an Ajax handler script where I load a $nodeId and output the result in default (full) view:

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

Now I need to display the teaser (summary or trimmed) as well.

I tried this so far, without success (summary is filled in content):

1.
$item = $node->body['en'][0];
$output = field_view_value('node', $node, 'body', $item, 'Teaser');
echo $output; (blank)

2. echo $node->body['eng']['0']['summary']; (blank)

A solution from this question but not working:

3. $output = truncate_utf8(strip_tags($node->body['eng']['0']['summary']),200,true,true);
echo $output; (blank)

Curious is that var_dump($node->body['eng']['0']) displays and array containing value (of body), summary, clean_summary and other elements and the summary has the necessary value filled. But as in example 2 I can't directly access it, it's coming up blank on display.

Tips, please?

Thank you.

Clive
  • 36,918
  • 8
  • 87
  • 113
Vlad Ghita
  • 43
  • 1
  • 4

2 Answers2

10

The correct way to do this without accessing the value directly (so you automatically get an internationalised version I think) is shown below:

$node = node_load($nid);
$body = field_get_items('node', $node, 'body');
$teaser = field_view_value('node', $node, 'body', $body[0],'teaser');

To output the $teaser value, you need to pass it to the render() function

print render($teaser);

JC

jonshot
  • 735
  • 6
  • 12
5

I guess this a multi-lingual site otherwise you'd probably find what you were looking for in $node->body['und'][0] (i.e. und, the undefined language code)?

Your 1st solution should work, only you've used Teaser instead of teaser, I'm pretty sure the view mode is case sensitive. Also you've got $node->body['en'][0] (language code of en) whereas you've used the language code eng in all of the other examples...could that be the problem?

Your 2nd solution would simply have to work if $node->body['eng']['0']['summary'] was not empty so I'd check again that your var_dump() is producing accurate results (are you inspecting the output of var_dump() immediately after you call $node = node_load($input); to make it a fair test, for example).

Similarly if your 3rd solution is outputting a blank string it means $node->body['eng']['0']['summary'] absolutely must be empty.

I'd strongly recommend installing the devel module and using the dpm() function to print a nicely formatted, hierarchical view of objects/arrays for inspection. If you can't do that Drupal 7 has a debug() function which does something similar. The output of both of these functions is printed to the standard Drupal message space.

Hope that helps!

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Ok, thank you for your answer, that helped me.
    First of all I fixed the typos with `eng` and `en` in the question (in code they are fine).
    All solutions stated above work fine, except solution 1 where I had to change from `Teaser` to `teaser`, but I was echoing in the wrong place. After I changed the place where I dump the data, it started working.
    – Vlad Ghita Sep 01 '11 at 08:01