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!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

Category: task » support

You'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.

valiant_falstaff’s picture

Good 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.

mastoll’s picture

It looks like you haven't figured this out yet. I have the same need. Any leads yet?

jnorell’s picture

valiant_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...

jnorell’s picture

Issue summary: View changes

Some 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.

jnorell’s picture

FileSize
8.72 KB
72.74 KB

Nevermind on the Entity Reference, I have that in my mix, but I think your view is working now with this additional relationship (see screenshot).

jnorell’s picture

One 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.

jnorell’s picture

Issue summary: View changes

I reworded a few sentences to make the points more clear.

valiant_falstaff’s picture

Issue summary: View changes

Made a correction on the point I was trying to make in paragraph 2.

mastoll’s picture

jnorell, what aggregation settings are you using? How do they play into the solution?

valiant_falstaff’s picture

jnorell, 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?

valiant_falstaff’s picture

Issue summary: View changes

changed "I have 8 jobs in the configuration" to "I have 8 pieces of equipment in the configuration"

Status: Active » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

CProfessionals’s picture

I 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!

shamerlinck’s picture

I 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!!