开发者

Drupal 7 get teaser with field_view_value()

开发者 https://www.devze.com 2023-04-01 06:22 出处:网络
I have an Ajax handler script where I load a $nodeId and out开发者_如何学运维put the result in default (full) view:

I have an Ajax handler script where I load a $nodeId and out开发者_如何学运维put 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.


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号