I tried to create a view of Content, and have their Field Collection items shown (or filtered, etc.) but this was not possible, because a relationship was needed. I've written a patch that allows this to happen, and creates the relationship on the field that is the Field Collection.

Please let me know if this works for you (it does for me). Views 3 confuses me, but I think what I have provided here is fine, maybe a few API tweaks that will make the code function better. If you know of any, and want me to fix the patch then let me know and I will make the necessary adjustments :)

Comments

NicoH’s picture

Thanks very much! It works for me, too.

hnln’s picture

Seems to work ok here 2.

yevhen’s picture

Status: Closed (fixed) » Active

it's work, great!

fago’s picture

Status: Active » Needs review
StatusFileSize
new1.86 KB

Thanks!

Views provide a dedicated hook for field modules to alter the views integration, so I think we should use that. Also I'd prefer using a "views" directory, and currently there is only ALPHA api in views.

Patch that works for me attached. Please test.

fago’s picture

Status: Needs review » Fixed

as it worked fine in my tests, I've committed it. Please test and report any problems.

mariagwyn’s picture

Very nice! I was just fiddling around trying to figure out how to pull the fields within the collection fields. This solved the problem, no errors so far.

bojanz’s picture

Just a small note, the api version should be 3 now, not 3.0-alpha1. Confirmed by merlinofchaos.

hartogsmith’s picture

thanks for this module and this patch.

stuck on getting can someone give me a nudge in the right direction here...
my field collection results are something like:

kittens by color

calico 3
tabby 5
midnight 2

but i want my view to display just something like:

all kittens

total 10

that would require a relationship and aggregation? any help at all would be appreciated.

RobW’s picture

I would use the amazingly useful Computed Field module. You could also create a custom function in your template.php to do the math and call it along with some theming in your view row tpl, but the computed field is more robust and also readable by views, so you could sort or filter by it if you wanted to.

Status: Fixed » Closed (fixed)

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

SaurabhSinha’s picture

I'm sure I'm doing something wrong... but I just can't get Content-to-FieldCollection relationship to work!!
I have tried to manually update 7.x-1.0-beta1 with the patch & I have also tried doing a clean install of the latest dev version..

My setup is simple:

Node A
- Field 1
- Field 2
- Field Collection -- (Multiple Values)
--- Field 3
--- Field 4

All I want is a simple view

Field 1 | Field 2 | Field 3 | Field 4

I'm creating a view of content and trying to add a relationship to the relevant field_collection, but don't see any field collections under relationships. However, if I create a view of type field collections, I see field_collection-->field relations.

Can someone please help or point out if I'm doing something wrong. Thanks

Jason Dean’s picture

I'm having a similar problem, using latest dev of Field Collection and Views.

I can create a view of content and add a relationship to the field-collection item no problem. Then I add collection fields to the view and use the relationship, but nothing shows.

As above, if I create a view of type field-collections it works fine. hmm.

GlitchFreak’s picture

Is this patch part of the current -dev release?

bryancasler’s picture

Status: Active » Closed (fixed)

it appears as is if it is a part of the latest dev, but I too am having problems figuring out how to implement the relation

prabhatjn’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta3
Status: Closed (fixed) » Active

Hi,
I am opening this issue in order to get some direction. I have problem same as in comment #11. Can some please help me how to solve that.
When I use relationship then I get multiple results...
Thanks,
P.

boobaa’s picture

Status: Active » Needs review
StatusFileSize
new3.39 KB

Having the same problem while working on a school's site. Data structure is as follows: we have a 'registration' content type with a multivalue 'classes' field in it. This field is actually a field_collection_item with several (singlevalue) fields in it: 'class', 'teacher', etc. Customer expected the 'classes' field of the 'registration' nodes displayed as a comma-separated list of classes – which is not something field_collection.module could have. Additionally, the customer wanted a list of registrations, where each row should contain the 'registration' node's title and the same comma-separated list of classes.

Diving into the code we had to realize that while multivalue field_collection_items are supported, but we cannot display only one field of them (though I must admit that there are plenty of choices there, and we do have tried most of them). Thinking about it we had to realize that all we need is a configurable field_formatter for the field_collection_item field where one can select which field is to be displayed.

Attached is the patch that handles this, and this approach seems to be an answer for #11 as well.

PS. IIRC there are some other issues in this queue which can be solved by this approach, but I don't have the time right now to look them up again.

dr-upal’s picture

dr-upal’s picture

#16: field_collection-1172372-16.patch queued for re-testing.

stayontherun’s picture

#16 works for me

pieterdc’s picture

Tried patch from comment #16, and I was able to choose a (sub)element of the field collection to show in my view, instead of having to show them all.

cameron prince’s picture

#16: field_collection-1172372-16.patch queued for re-testing.

czigor’s picture

Version: 7.x-1.0-beta3 » 7.x-1.x-dev
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new3.39 KB

Patch updated for dev. (No change apart from line numbers, so I mark this RTBC)

thoughtcat’s picture

I don't use a VCS unfortunately, is there anyone who can supply a patched version of the module file or tell me how to patch it without using Git?

Macronomicus’s picture

yay!
#22 works perfectly. even on the latest release which is newer than the latest dev.
Cheers!

@thoughtcat Just apply the patch, you dont need a vcs to do it.
From the command-line its as simple as going into the field_collection directory... then...
wget http://drupal.org/files/field_collection-1172372-22.patch
patch -p0 < field_collection-1172372-22.patch

alltooeasy’s picture

Thanks for this - any further documentation on the changes/implementation would be great.

Thanks macrocosm! http://drupal.org/node/60818 Might be useful for Mac users when patching this up.

matkeane’s picture

Many thanks to Boobaa & czigor for the very useful patch. It makes it very easy to break out just one field from the field_collection and display multiple values as a list - exactly what I was looking for!

Now if only I could work out how to filter on those values... I have a User Reference field in the collection but I can't, for the life of me even after fiddling around with all sorts of combinations of Views Relationships, figure out how to add the Uid as a Contextual Filter.

[Update] Got it! But it requires two relationships, which is far from intuitive (for me, at least). Just in case anybody else is running into this: First add a Relationship to the Field Collection itself (filter by 'content'). Then add a new Relationship - filter by 'Entity Reference' and then choose the one which provides 'A bridge to the User entity', and set this relationship to require the first one you added. Now you can add a 'Contextual Filter' to Uid, using the second relationship, and it works! And setting both relationships as 'required' helped filter out duplicates for me.

Anyway, apologies for hijacking this thread with an only-slightly-related matter, and thanks again for the patch!

alltooeasy’s picture

Thanks, patch worked a treat!

kristen pol’s picture

I'm having trouble getting this to work (I have applied the patch from #22). Could someone provide some info on how they managed to get it to work?

Example:

Field collection Booga
- textfield Ooga

Content type Foo
- field collection Bar field of type FC Booga - set to unlimited (i.e. they will be able to enter an unlimited number of Ooga textfields)

Node 567
- has Ooga textfields filled in:
* abc
* 123
* xyz

Need the view to show the Ooga textfields:

* abc
* 123
* xyz

when specifying the nid=567 (using contextual filter).

I have:

  1. Added contextual filter for nid and set the default in case it can't find it.
  2. Added a relationship for the FC Bar field (make it required).
  3. Added an Ooga field to my fields list.

I see nothing. It's probably something obvious but I'm tired and not thinking straight... Thoughts?

[UPDATE] <facepalm> It helps if the data is really where you think it is... doh! Anyway, the steps above might help someone else who is setting this up ;)

bcobin’s picture

#22: field_collection-1172372-22.patch queued for re-testing.

grootte’s picture

fago’s picture

Status: Reviewed & tested by the community » Closed (fixed)

Please do not re-open and take over issues like this - open a new one instead such that the new issue describes what the patch is about....

Setting back to closed. Please move content to a new issue.

spessex’s picture

I am currently on latest 7.x-1.0-beta5 and have some very complicated (nested) Field Collections and cannot now (having used Field Collection) pull in information from fields that have a connected relationship (as in the original posted). I'm pretty new to Drupal so do not understand how patches are applied but even though I would have expected my version of this module to have this patch applied (#22), I have applied the patch (to try and fix the solution) and it has applied correctly but still doesn't fix the problem. Can anyone advise as I really need to use Field Collection to be able to pull information into views through a 'relationship'?

czigor’s picture

kristen pol’s picture

Issue summary: View changes

Updated issue summary.