Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How do I get the value of a field collection field in Views? I realize that http://drupal.org/node/1047784 is an ongoing views integration thread. With PHPCustom field print_r($data), I can see the value(s) of the field collection. How can I print just one of the fields in the collection?
Example:
Shorts (field collection in node)
>>Brand (a field in field collection Shorts)
>>Color (a field in field collection Shorts)
In views, I just want the values for Brand per each nid.
Comments
Comment #1
hellomobe CreditAttribution: hellomobe commentedNot meaning to bump this; just adjusting the title to make it more accurate. I am also wondering how to get the individual fields print in node.tpl without using Views.
Comment #2
RobW CreditAttribution: RobW commentedI wrote a little about this in #1107214: Field Collection tpl overrides and accessing field collection field values from node.tpl and #1155752: How to theme field collection?. The latter is more about embedded field collections, and gets a little confusing.
It's a little bit more involved than just printing things in the node.tpl. The short answer is you use entity_load() on your field collection value, then go one level down in the array with a foreach statement, then you have an entity object that contains your field collection fields that you can use just like the node object.
Comment #3
fagoor you can use the entity API wrappers...
Though, I'd suggest doing such kind of stuff in the template preprocessor...
Comment #4
RobW CreditAttribution: RobW commentedThanks fago, I didn't know about entity wrappers. They look simpler and more powerful -- and I'm guessing they're superior in performance as you can load them once and get the whole chain, as opposed to calling entity_load() multiple times. Any other benefits we should be aware of?
I'll give them a shot on my next project, and then contribute a node preprocess function and some theming docs back.
Comment #5
fago>Any other benefits we should be aware of?
hm, it automatically uses the right default field-language, so you don't have to care.
>I'll give them a shot on my next project, and then contribute a node preprocess function and some theming docs back.
Sounds great!
Comment #6
hellomobe CreditAttribution: hellomobe commentedRobW and fago, thank you for the replies. It did answer my question. However, to the next step/level, I'm trying to have it also evaluated the min value of the fields.
For example:
Field Collection[0]
field_sample -- value = 2.00
field_example
Field Collection[1]
field_sample -- value = 3.00
field_example
I'd like to get an array of the field_sample(s) so that I can do a min(). It seems you have to do a foreach on the field collection and then field_sample (??). Is this possible with the wrapper - please explain? Unfortunately I'm not a programmer and I'm not successfully hacking it with my research and limited knowledge.
Edit: Figured it out finally; I was missing the field after the $i.
fago - the wrapper is very handy!
Comment #7
tim.plunkett#6 implies this is fixed.
Comment #9
CarbonPig CreditAttribution: CarbonPig commentedSorry to open this up again, but I'm a bit confused about how to retrieve one of the filed-collection fields in a view.
Do I need to add some sort of a custom php field to the view or is there a way to do this via the views UI.
Thanks ahead of time,
CarbonPig
Comment #10
sachbearbeiter CreditAttribution: sachbearbeiter commentedi think some people (including me :) have problems how to print field values ...
maybe it would be helpful, if somebody would solve #1294536: Output in node--foo.tpl.php and we put it as a complete example (because it's so typical) without abstraction into the issues ...
@9 i'm sorry - i also have no solution for field based views ...
Comment #11
topdawg CreditAttribution: topdawg commentedI get no results when putting into my node--content_type.tpl - is there anything I need to have additional before this statement
Comment #12
guybedford CreditAttribution: guybedford commentedFor inclusion into the node template, I provided the code that works for me here: http://drupal.org/node/1294536#comment-5362592
Comment #13
robcarrI'm a bit confused on this working with the Views field template (views-views-field--myview--mycollection_field.tpl.php). The only variables available to this template are (as commented in views-views-field.tpl.php):
Trying to render just a single field from within my field collection object, and don't seem to be able to traverse down the $row object array to grab the necessary value. Should entity_metadata_wrapper() be used to wrap a field collection object (couldn't get it to work for me)?
Any nudges greatly appreciated
Comment #14
hellomobe CreditAttribution: hellomobe commentedWith the dev module installed, you should be able to
dsm($row)
inside your views tpl to see the array. This thread is relating more to getting the results in a node.tpl (without a views query)Comment #15
robcarrNot exactly elegant, but this seems to be a way to get values of a field collection item into a View (using the 'Field Collection Items' Formatter in your field for the View:
And this code went into a Views theme template (views-views-field--myview--mycollection_field.tpl.php) - which you can work out in your view in Advanced > Theme - Information.
If anyone has a better solution, please post. Probably could do with some more intuitive variable names. (*EDIT* slight tweak to code and added dsm() statement - commented out)
I hope this should answer #9 now, so maybe this issue can be closed?
Comment #16
Pepe Roni CreditAttribution: Pepe Roni commentedIn the view you can add a relationship to the field collection and than access all fields of this field collection thru this relationship. Where is the problem?
Comment #17
robcarrIf you are comfortable with relationships, you can add a Field Collection relationship in a View to extract specific values. But the question raised was how to present a field of a Field Collection object using a tpl.php. I think.
Job done?
Comment #19
Morasta CreditAttribution: Morasta commented#12 worked for achieving this in the node template, node--foo.tpl.php. Added a post for handling unlimited values: http://drupal.org/node/1294536#comment-5644956
Comment #20
dgastudio CreditAttribution: dgastudio commentedworking code for print field collection items in views
Comment #21
partyp CreditAttribution: partyp commented@wbue I am able to access specific fields by adding a relationship, but the problem is that it only prints that specific field in the FIRST field collection. I have multiple entries of the collections and I want to print a specific field out for each entry, not all of the fields.
Comment #22
partyp CreditAttribution: partyp commentedSo, i found out what my issue was... I was only displaying 1 item in the pager view settings. Duhh :(
Comment #23
ghan CreditAttribution: ghan commentedFago, your code snippet works like a charm for most field collection fields. Thanks!
I'm having trouble adapting it to images... fields that are images (ones that get uploaded from admin UI) display as arrays:
so for example:
results in:
Array ( [fid] => 46 [alt] => [title] => [width] => 116 [height] => 83 [uid] => 1 [filename] => Screen shot 2012-05-10 at 2.43.17 PM.png [uri] => public://Screen shot 2012-05-10 at 2.43.17 PM.png [filemime] => image/png [filesize] => 14662 [status] => 1 [timestamp] => 1336676039 [rdf_mapping] => Array ( ) )
Any suggestions to render the image instead?
Thanks!
Comment #24
ghan CreditAttribution: ghan commentedFigured it out:
file_create_url($thumb['uri']) creates a web-accessible URL.... then just wrap it in an tag.
Comment #25
MickL CreditAttribution: MickL commentedto print a certain field of a field collection in a node.tpl.php do:
Comment #26
jmix CreditAttribution: jmix commentedbenchi, it seems that there's a problem with your code in #25, can you help me to make this work ?
Comment #27
MickL CreditAttribution: MickL commenteddelete the last "?>"
Comment #28
RobW CreditAttribution: RobW commentedIf it's in a tpl file, that fixes the syntax. Haven't tested if the code actually works though.
Comment #29
Elehas CreditAttribution: Elehas commentedWorking off of #20 is what got mine to work. Thanks so much!
Comment #30
Karan Sen CreditAttribution: Karan Sen as a volunteer and commentedTo load the field collection items in node please see the module Field Collection load. The link to the module is https://www.drupal.org/project/field_collection_load.