I’m having trouble using fields of nested field collection items in Views. If a field collection item isn’t nested within another field collection item, then I can get its fields displayed by views no problem (by creating a views relationship as explained in this Drupal Stackexchange page on integrating Field Collection Module in Views), but I can’t get it to work if the field collection item is nested within another field collection item. Here’s my example:
I have a content type with a field collection item called “Job.” “Job” has two fields in it, both of which are also field collection items, “Job Info” and “Equipment Info”. “Job Info” has two text fields, “Job Name” and “Job Date”, and “Equipment Info” has a text field called “Equipment Name”, and an integer field called “Equipment Value”. The attached pic "Field Collection Illustration.jpg" illustrates the fields.
My goal is to have a view that simply lists the value of each piece of equipment; the view would look like this for four pieces of equipment:
Value: $500,000
Value: $300,000
Value: $235,000
Value: $160,000
When I set up this view as pictured in the screenshot "View.jpg" the values in the view are blank and there are an incorrect number of them (I have 8 pieces of equipment in the configuration, but only 3 are showing up). The view ends up looking like the attached pic "Valuesblank.jpg".
My hunch is that I need to create a second relationship in the view, but it’s still not working after trying lots of relationship combinations. To make things more illustrative for myself and others reading this post, I have set up a live mock site with this example (which I hope to maintain indefinitely for the benefit of others) at www.shakespeare79.byethost31.com/ Feel free to login with the username ‘helper’ password ‘drupal’ to edit the view if you know how to get it to work. (Let me know if an additional module needs to be installed in order to achieve the task and I’ll install it). Thanks!
Comment | File | Size | Author |
---|---|---|---|
#8 | 2 Field Collections (1 nested) field mapping.png | 128.44 KB | valiant_falstaff |
#8 | 2 Field Collections (1 nested) View Settings.png | 306.95 KB | valiant_falstaff |
#8 | 3 Field Collections (2 nested) field mapping.png | 184.46 KB | valiant_falstaff |
#8 | 3 Field Collections (2 nested) View Settings.png | 358.96 KB | valiant_falstaff |
#5 | view_settings.png | 72.74 KB | jnorell |
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedYou're right, you definitely want two relationships, because what you have there is trying to display the fields from the first tier relationship. There *should* be another relationship that you can chain down from the field collection relationship. Which should be named based on the bundle name of the field collection, which I believe is based upon the fieldname. I'm a little hazy on that part. However, your graphic depicts very nicely what the relationship tree looks like. You should thus have two relationships. One that relates from the node to the job field collection; and then one that relates from the job field collection to the equipment field collection.
Comment #2
valiant_falstaff CreditAttribution: valiant_falstaff commentedGood to know I'm on the right track with the second relationship idea. I'll keep experimenting with it and update the www.shakespeare79.byethost31.com/ mock site if I'm able to figure it out so that others can see exactly how to do it.
Comment #3
mastoll CreditAttribution: mastoll commentedIt looks like you haven't figured this out yet. I have the same need. Any leads yet?
Comment #4
jnorell CreditAttribution: jnorell commentedvaliant_falstaff, do you have the entityreference module enabled? Judging by the options available under add Relationships on your demo site, I'd guess you don't. Might give that a try and see what else shows up.
For what it's worth, I'm trying to figure out this same scenario. I almost suspect it's a bug in nested Field Collections, but I do have more options to play with under my Relationships .. they just don't work to get at the second-level field collection fields (eg. field_value in your demo site). Or maybe I'm doing something wrong...
Comment #4.0
jnorell CreditAttribution: jnorell commentedSome URL links to illustrative images were contained in the original message, but I discovered that the "attach file" feature was working in the Drupal issue system, so I deleted the links in preference of the in-house system.
Comment #5
jnorell CreditAttribution: jnorell commentedNevermind on the Entity Reference, I have that in my mix, but I think your view is working now with this additional relationship (see screenshot).
Comment #6
jnorell CreditAttribution: jnorell commentedOne more note, I did finally gain access to all my fields in the view, of a content type with a field collection containing an entity reference to another nested field collection. So bug found, I just had to get all the relationships and aggregation settings right.
Comment #6.0
jnorell CreditAttribution: jnorell commentedI reworded a few sentences to make the points more clear.
Comment #6.1
valiant_falstaff CreditAttribution: valiant_falstaff commentedMade a correction on the point I was trying to make in paragraph 2.
Comment #7
mastoll CreditAttribution: mastoll commentedjnorell, what aggregation settings are you using? How do they play into the solution?
Comment #8
valiant_falstaff CreditAttribution: valiant_falstaff commentedjnorell, great solution in comment #5, thanks! I’ve taken your concepts and applied them to another view I created in the www.shakespeare79.byethost31.com/ mock site called “Top Companies by Revenue.” This new view has three field collection layers (the original view has just two), so drupalers can compare the two views to better understand the common concepts behind creating a view of nested field collection items, no matter how many nested layers they have. I also have illustrations of these concepts in the attached images, and these images also contain specific instructions for creating the views.
Good question by mastoll in #7, can you expound on your experience with the entity reference module, jnorell?
Comment #8.0
valiant_falstaff CreditAttribution: valiant_falstaff commentedchanged "I have 8 jobs in the configuration" to "I have 8 pieces of equipment in the configuration"
Comment #11
CProfessionals CreditAttribution: CProfessionals commentedI know this is closed I just wanted to say thank you. Your documentation really helped me! I was looking for "Referenced Entities" in the relationships and your images illustrated that the field collection was actually "Content". I had been working hours on this. Thank You!
Comment #12
shamerlinck CreditAttribution: shamerlinck commentedI concur with CProfessionals - even years later, this has been the best crash course in handling nested relationships in views available out there. Well done @valiant_falstaff. Thanks!!