Drupal 7 gets a teaser with field_view_value ()

I have an Ajax script handler where I load $nodeId and $nodeId default result (full):

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

Now I need to display the teaser (final or cropped).

I have tried this so far, without success (the resume is filled with 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) 

The solution is from this question, but does not work:

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

Curiously, var_dump($node->body['eng']['0']) and an array containing value (bodies), summary , clean_summary and other elements are clean_summary , and summary has the necessary value. But, as in example 2, I can not directly access it, it appears on the screen.

Tips, please?

Thanks.

+4
source share
2 answers

I assume this is a multilingual site, otherwise you will probably find what you were looking for in $node->body['und'][0] (ie und , the language code is undefined)?

Your first solution should work, only you used Teaser instead of Teaser , I am sure that the view mode is case sensitive. Also, you have $node->body['en'][0] ( en language code), while you used the eng language code in all other examples ... maybe there is a problem?

Your 2nd solution just should work if $node->body['eng']['0']['summary'] not empty, so I checked again that your var_dump() gives exact results (you check var_dump() output immediately after calling $node = node_load($input); for example, make it an honest test).

Similarly, if your third solution displays an empty string, then $node->body['eng']['0']['summary'] must absolutely be empty.

I highly recommend installing the devel module and using the dpm() function to print a beautifully formatted hierarchical representation of objects / arrays for validation. If you cannot do that, Drupal 7 has a debug() function that does something like this. The output of both of these functions is printed into the standard Drupal message space.

Hope this helps!

+5
source

The correct way to do this without directly accessing the value (so that you automatically get an internationalized version):

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

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

 print render($teaser); 

x

+10
source

All Articles