You have a multivalue field. You want to display only the first image. You have a problem.
Views does it by cloning $entity and removing the deltas it doesn't need.
Token does it by defining it's own view mode: http://drupalbin.com/17204.
So, we need an extra param. Call it $index. Call it $delta. Just call it something :)
Filing against 7.x in hope we can get something before D8. Otherwise we'll have to continue with workarounds...
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | field_view_value_doc-1017850.patch | 1 KB | yched |
Comments
Comment #1
dave reidToken making its own view mode isn't really related, we just made a duplicate copy of field_attach_view() that returns a specific index's value output rather than all of them.
Comment #2
bojanz commentedThanks for the clarification.
In any case, we can do better :)
Comment #3
yched commentedUnless I'm missing something, that's what field_view_value() is about - return the render array for a single formatted value (with no field wrapping markup) ?
Comment #4
catchI thought field_view_value() was for this as well, has this been tried?
Comment #5
bojanz commentedI had no idea field_view_value() existed.
And it actually uses the same approach dereine & I implemented in Views (clone the entity, remove all field deltas except the one being displayed).
Plus, the "no wrapping markup" tip is gold.
I guess that's it then. Thank you for your help.
Comment #6
yched commentedIn fact, field_view_values() was written specifically to support Views' use case :-)
Attached patch adds a hint about field_view_value() in the phpdoc for field_view_field().
Comment #7
antiorario commentedIt seems to me that field_view_value() isn't extracting anything. All I get when I use it is something like:
Particularly the last part of the function doesn't seem to be doing what it claims to do:
Does anyone else have the same feeling, or am I just not understanding this?
Update: see my next comment for a solution.
Comment #8
antiorario commentedHere's a version of field_view_value() that works:
Beside the final part that, as I mentioned in #7, wasn't really doing anything, I also noticed that
<?php $clone->{$field_name}[$langcode] = array($item); ?>was removing the actual field information from the object—the main reason why the function wasn't returning anything.Do we really need $langcode as argument, if it gets systematically owerwritten in the function?
Also, I removed the part where $entity gets cloned, as it seemed pointless. Maybe there was a good reason for the cloning, and if so I'll be happy to know.
One last thing: I'm not sure we need to specify, as a condition,
<?php if ($field = field_info_field($field_name)) { ?>. Wouldn't<?php if (field_info_field($field_name)) { ?>be enough? After all, $field never gets called again.Comment #9
yched commented@antiorario : You're calling field_view_value() with $item param set to 3. Re-read the phpdoc of the function, $item should be something like $node->field_image['und'][3]. http://api.drupal.org/api/drupal/modules--field--field.module/function/f....
Gives :
and after drupal_render() :
<img typeof="foaf:Image" src="http://localhost.d7/sites/default/files/field/image/my_image.png" alt="" />Also, the various changes proposed in #8 are incorrect. Main reason is : you can't assume a formatter will return one sub render array per incoming value in $entity->field_name[$langcode]. Some formatters will munge all multiple values in a single piece of HTML (e.g points in a graph, or markers on a map). The only assumption you can make is that you'll get a render array with numerically indexed child elements, starting at 0 - see http://api.drupal.org/api/drupal/modules--field--field.api.php/function/....
The correct approach is to alter the incoming $entity->field_name[$langcode] and pretend there is only one incoming value. Since we alter $entity, we need to clone it.
Comment #10
antiorario commentedThanks, I'll re-read the instructions. Seems that's all I've done today, but I guess I missed some stuff.
Comment #11
dave reidDoc patch in #6 looks great to me.
Comment #12
plachComment #13
webchickCommitted to 8.x and 7.x. Thanks!