Currently, the Field_collection module implements item weights using deltas, which to my understanding, is incorrect.
Part of this problem stems from the fact that field_collection items are loaded from the field_collection_item
table instead of the field that it references.
Deltas are supposed to reference nothing more than the fact that more than one field instance exists.
For instance: I have a Profile2 field-collection called "Education". The source entity allows unlimited data in this field. The collection-item entity has several fields attached to it.
I noticed the .install doesn't provide a "weight" field for the field_collection_item. It also seems like this module removes all field-collection values for the present entity and re-inserts them using their "delta" as the order.
The reason this problem starts to crop up in views, is that the "delta" is a "field", not a pseudo-field like weights. So, when the view processes, "delta" is selected as data, and Views tries to use the field alias to handle sorting. The net effect is that Views selects delta information when it shouldn't be, and the items pulled from the DB don't match our expected results.
Comments
Comment #1
charlie-s CreditAttribution: charlie-s commentedAlso looking for this feature.
Comment #2
rerooting CreditAttribution: rerooting commentedChanged some of the issue information so that its more descriptive
Comment #3
webguy2000 CreditAttribution: webguy2000 commentedI need this feature as well.
Comment #4
rerooting CreditAttribution: rerooting commentedYes it looks like it's using delta, thus when items are removed or on edit, it looks like the array of field collection items is just reordered. I can see why they would do it this way (a very simple approach) but it makes views handlers very tricky.
Possibly, one could write a views handler that uses the delta and the host entity information to determine a 'weight' for any field collection item using one of the existing functions.
Comment #5
RobW CreditAttribution: RobW commentedI think instead of creating a new way to accomplish an existing task, we should fix field collection by adding weights in addition to delta.
Comment #6
P2790 CreditAttribution: P2790 commentedYeh I would really find having weights very useful. It would allow me to integrate into draggableviews
Comment #7
joelcollinsdc CreditAttribution: joelcollinsdc commentedso... there is no way to use views to print out items of a field collection in the order that they appear?
Comment #8
flinth CreditAttribution: flinth commentedI did a view like this and found out an explanation:
In the database there is no weight stored, only the delta which is used to indicate the order of the elements.
So if you need to sort the field_collection items in your views' result by the order they are set, you'll need to use the delta as you'd use the weight.
Comment #9
CarbonPig CreditAttribution: CarbonPig commented#8 worked for me. Thanks for the tip @milanith
Comment #10
mudsurfer CreditAttribution: mudsurfer commentedHi milanith and CarbonPig - I'm feeling quite daft - but where did you find the delta field?
I have a view that just displays the content of a field collection (no other related data). But I cant find a way to display the view in the same order as they are set.
I've resorted to added a an extra "sort order" integer field on the field collection.
Comment #11
Watergate CreditAttribution: Watergate commentedI have just provided a comment to a similar question in the Field Collection Views module here
In this way no extra sort order fields are necessary.. just a required relationship to the parent entity.
Hope this can help you out.
Comment #12
antpowerWD CreditAttribution: antpowerWD commentedCan you explain this really simply, I don't fully understand the advanced stuff with Views and need to read up on it, but I have a grid of boxes using the field collection views module and I can't reorder any of them by weight. There are two views one for field collection itself and one for the 'boxes'. I've tried different configs, but nothing can enable them boxes to be re-ordered as my client would like.
Please help.....
Ant:-)
Comment #13
mareks CreditAttribution: mareks commentedI only needed to add a relationship to my field collection item. AND I also had to check the "Require this relationship". I don't have anything in sorting criteria but my view still respects the row order (weight) in the collection.
Comment #14
mikemccaffreyThis problem seems more relevant to the Field Collection Views module, where this duplicate issue exists:
https://www.drupal.org/node/1444798
Comment #15
FrancewhoaAnother option we found:
Thanks to animare, mareks, mikemccaffrey, Watergate for the inspiration
Comment #16
FrancewhoaAdded duplicate ticket
Comment #17
wjhessels CreditAttribution: wjhessels commented#15 works
Created a relationship to the top level entity in the view that generates the output for the Field Collection field. This relationship is with 'Field collection item: Entity with the {Field Collection field}' and marked required.
Make sure that you have a relationship with the nid that is used. With contextual filters or filter criteria with that specific nid.
Comment #18
SalAG CreditAttribution: SalAG commented#13 worked perfectly, for me. This orders the fields collections fields in the order they are entered in the node edit mode.
If you 'sort by' one of the fields it will end up being alphabetical, which may not be what you are looking for.
**coming back after a few days**
The list reverted to a 'most recently created' order after a content curator added a few more items.
I added Content: Accordion Fields:delta (asc) to the Sort Criteria, with no other sort, and that seems to have fixed it.
Comment #19
mxrr CreditAttribution: mxrr commentedI don't think this issue should be closed. I'm not using Field Collection Views module (reasoning for closing this in #14) and I still experience this issue when listing items via entity_load.
I might have structured my page with a very different reasoning than Drupal expects? I have a different page template for this special content type that uses a Field Collection. I get the items using entity_load and print them in a loop. The ordering of the items when fetched does not match the ordering shown to the user when editing the items. The weight is stored somewhere, since it's perfectly possible to reorder the items and save the new order and such. But I'm unable to access this weight when I load them in the template.