php - Drupal 7 get teaser with field_view_value()

667

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 thatvar_dump($node->body['eng']['0']) displays and array containingvalue (of body),summary,clean_summary and other elements and thesummary 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.

580

Answer

Solution:

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

879

Answer

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 usedTeaser instead ofteaser, I'm pretty sure the view mode is case sensitive. Also you've got$node->body['en'][0] (language code ofen) whereas you've used the language codeeng 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 yourvar_dump() is producing accurate results (are you inspecting the output ofvar_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 thedpm() function to print a nicely formatted, hierarchical view of objects/arrays for inspection. If you can't do that Drupal 7 has adebug() function which does something similar. The output of both of these functions is printed to the standard Drupal message space.

Hope that helps!

People are also looking for solutions to the problem: php - Form encoding problem in different browsers

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.