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

charlie-s’s picture

Also looking for this feature.

rerooting’s picture

Title: Views support: Sorting collection items is broken » Views: Field Collection Item weights not available for sorting
Category: bug » feature

Changed some of the issue information so that its more descriptive

webguy2000’s picture

I need this feature as well.

rerooting’s picture

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

RobW’s picture

I think instead of creating a new way to accomplish an existing task, we should fix field collection by adding weights in addition to delta.

P2790’s picture

Yeh I would really find having weights very useful. It would allow me to integrate into draggableviews

joelcollinsdc’s picture

so... there is no way to use views to print out items of a field collection in the order that they appear?

flinth’s picture

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

CarbonPig’s picture

#8 worked for me. Thanks for the tip @milanith

mudsurfer’s picture

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

Watergate’s picture

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

antpowerWD’s picture

Issue summary: View changes

Can 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:-)

mareks’s picture

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

mikemccaffrey’s picture

Status: Active » Closed (duplicate)

This problem seems more relevant to the Field Collection Views module, where this duplicate issue exists:
https://www.drupal.org/node/1444798

Francewhoa’s picture

Another option we found:

  1. Using the View. Add a "Relationship" to the Field Collection.
  2. Still using that View. You will now be able to add individual "Fields" from that Field Collection.
  3. Using the View add a "Sort criteria". Set a default sort.
  4. This is optional. Using that View. Expose the "Sort criteria" you just added. Users are now able to change the default sort to their liking.
  5. This is also optional. If you need to view all fields in the Field Collection but also need them sort by one or more field(s). Using the View add the full Field Collection under "Fields". Any "Sort criteria" you added above still apply. The sort(s) you did in the step 3 and or 4 above.

Thanks to animare, mareks, mikemccaffrey, Watergate for the inspiration

Francewhoa’s picture

Added duplicate ticket

wjhessels’s picture

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

SalAG’s picture

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

mxrr’s picture

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