Search code examples
phpdrupaldrupal-7teaser

Drupal 7 get teaser with field_view_value()


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.


Solution

  • 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!