1

to render a field "view mode" (or "format") inside a node.tpl.php i use, for example:

<?php 
     print render(field_view_field('node', $node, 'field_myfieldname', array(
        'label'=>'hidden', 
        'type' => 'text_summary_or_trimmed', 
        'settings'=>array('trim_length' => 30),
    )));
?>

now i'd like to do quite the same thing, but inside a field-collection-item.tpl.php (for a field that is part of a field collection).

in my field-collection-item.tpl.php, i can render each field just like in a node;tpl.php:

<?php print render($content['field_myfieldname']); ?>

but if i try the first code above inside field-collection-item.tpl.php (to trim a field-collection field), it just doesn't work...

thanks you for helping !

1

I haven't tested this but I suspect your issue arises from the fact that the $node object is not available to that template file. You have a couple of options; write your own theme function, load the node object some other way (this might still not work though because of the way field collection works) or just trim the rendered value.

Something like this should work for you if you are just looking to trim the result to 30 characters:

<?php print trim(substr(render($content['field_myfieldname']), 0, 30)); ?>
1
  • thank you for answering ! due to my poor knowledge i'm not sure i'll be able to write my own theme function. i tried your suggestion to render a trim result but it did not worked on my tpl.php (didn't work in a node.tpl.php too). i forgot to mention that i'm using drupal 7, perhaps your code was for D6 ? – mix Jul 20 '12 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.