Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I think it would be very important to allow sorting of items.
When creating field_collection content in a node, you're only allowed to add content at the end of the list. What if you want to put some new item anywhere in the list?
Comment | File | Size | Author |
---|---|---|---|
#31 | field_collection-1142114-31.patch | 1.17 KB | tim.plunkett |
#6 | fieldcollectionsort.tar_.gz | 2.82 KB | dalin |
Comments
Comment #1
RobW CreditAttribution: RobW commentedIf you need it now, the subform widget allows drag and drop sorting in the node edit form: http://drupal.org/node/977890.
Comment #2
robertofusco CreditAttribution: robertofusco commentedHi RobW,
didn't mean the sorting of collected fields. I meant when I add field_collection to a content type, I'm only allowed to add instances of my Field_collection group on the content editing page and there I can only append one other field collection group to the others. I'd like to move "groups" and sort them.
Comment #3
RobW CreditAttribution: RobW commentedI think we may be talking about the same thing. If I misunderstand you, I apologize, and disregard the rest of this comment.
If I hear you correctly, you would like to be able to sort and re-order field collections with multiple instances (>1 or unlimited), instead of attaching them in order of creation. For example, in a node type with a field collection that contains artist, song title, and genre, you would like to add multiple instances of this field collection like so:
Node 1
-field collection field
--artist 1, song 1, genre 1
--artist 2, song 2, genre 2
--artist 3, song 3, genre 3
and then be able to add a new collection out of order, or re-order existing collections, like so:
Node 1
-field collection field
--artist 4, song 4, genre 4
--artist 1, song 1, genre 1
--artist 3, song 3, genre 3
--artist 2, song 2, genre 2
If you're looking to do that, check out the subform widget. It changes the way you add field collections, from the node view page to the node edit page, and allows you to drag and drop entire field collections to sort and re-order.
Comment #4
robertofusco CreditAttribution: robertofusco commentedYou're perfectly right.
It works exactly as you say.
I didn't figure out this after applying the subform patch because I've created one node before activating locale module in order to have multilanguage, and you know what? Content created before was hidden.
Now i've set language to neutral and it all works.
Thanks a lot.
Comment #5
RobW CreditAttribution: RobW commentedNo problem, glad to help.
Comment #6
dalinI disagree with the suggestion to use the subform widget. If your field collection has either a text area or several fields the subform widget is very unwieldly. A better way is to have a separate page for sorting just like there's a separate page for adding a new field collection item.
Attached is a proof-of-concept module that does this. If this technique works and is architecturally sound it could be rolled into field_collection module.
Comment #7
dalinComment #8
ocularinvasion CreditAttribution: ocularinvasion commentedI really like this. It's really clean and conforms to the design of the original module a little better.
I know you said it's a proof of concept, but is there any chance you're going to put a little more work into it? It works fine on nodes with field collections, but doesn't work on nodes with nested field collections—a field collection in a field collection.
Comment #9
dalinWell the first requirement for that would be a better bug report. "Doesn't work" does not qualify. ;-)
Comment #10
ocularinvasion CreditAttribution: ocularinvasion commentedSorry about that.
Here's the error message
Notice: Undefined property: FieldCollectionItemEntity::$nid in fieldcollectionsort_field_attach_view_alter() (line 38 of /home4/ameliajo/public_html/admin7/sites/all/modules/fieldcollectionsort/fieldcollectionsort.module).
Comment #11
webankit CreditAttribution: webankit commented+1
Comment #12
webankit CreditAttribution: webankit commented+1
Comment #13
Fidelix CreditAttribution: Fidelix commentedSubscribing...
Comment #14
kenianbei CreditAttribution: kenianbei commented+1
Comment #15
pacome CreditAttribution: pacome commentedSubscribing too
I'm using the last dev version (july 5) with the widget "embedded" activated.
It allows to sort the field-collections 1a, 1b, 1c like 1b, 1a, 1c within the create/edit page, but it doesn't apply yet those changes to the post itself..
Thanks a lot for this module by the way :)
-P-
Comment #16
WilliamB CreditAttribution: WilliamB commentedSubscribe, i've the same issue as pacome.
Backoffice sorting collection with drag and drop works, but the change are not saved.
Comment #17
WilliamB CreditAttribution: WilliamB commentedComment #18
dalin@WilliamB I suggest not changing the status if you don't know what it means.
Comment #19
WilliamB CreditAttribution: WilliamB commentedHum sure ok.
Sorting order of repetable fields with drag and drop is a core feature though.
And it's not working with collection, so i'd think it's an active bug instead of a requested feature but your call.
Comment #20
Fidelix CreditAttribution: Fidelix commenteddalin, I have to agree with him. IMHO this is a core feature so it makes sense to support it.
WilliamB, then you should have marked it as "Needs work".
Comment #21
byung CreditAttribution: byung commentedCan someone please upload the patch module file? I'm having issues trying to patch it myself. Thanks for your help.
Comment #22
kruser CreditAttribution: kruser commentedI can confirm that with the embedded collection, any sort changes do not get saved. Looking at the database, the rows are getting reordered, but delta and value columns remain the same.
Comment #23
tim.plunkettNot needs work/review until there's a patch.
Comment #24
swirtConfirming this. Changing the sort arrangement of a several instances of a field_collection on a node has no lasting effect upon save. The sort arrangement is back to its original upon editing the node.
Makes no difference if drag and drop sorting is used, or changing the actual weight numbers. Neither change is saved upon saving the node.
Comment #25
tim.plunkettComment #26
SolInvictus CreditAttribution: SolInvictus commentedWell I've two strikes against me. I'm new to PHP and Drupal, but in studying this problem it looks like either entity API's entity_save() should handle the translation of the form param _weight to delta or this should be handled by field_collection.module::save(). I can find nothing in the code path that would otherwise handle this. Am I headed in the right direction or into the weeds?
Comment #27
somanyfish CreditAttribution: somanyfish commented+1
Comment #28
adf1969 CreditAttribution: adf1969 commentedWARNING: I'm very new to Drupal 7.
Okay, that out of the way, here is what I did to allow my version of field_collection (2011-08-18) to sort:
1) Save the weight somewhere. I chose to do this in the field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) since that is where the $entity got saved to the field_collection Items list.
2) Ensure that the weight set, is saved in the correct order (technically in D7, this is supposed to be done in the hook_field_attach_submit, but it has been done historically in D6 in hook_field_presave, so that is where I put it since there was already work being done there to yank the $entity BACK out of the $items list, and plug the 'value' => item_id val into it)
That's it.
I could also have saved just the weight "value" instead of the *entire* _weight array...might be a bit cleaner...but it seemed at the time the easiest option to just save the whole thing (just in case I ended up needing some *other* value from _weight)
If someone who knows Drupal 7 better than me can let me know a better way, I'm all ears, but this works and hopefully pushes this ball along the road a bit further.
Next up....ability to "delete" values from field_collection (since there doesn't appear to be a "delete" button to delete a row like there normally is in multi-row table with drag-handles)
Andrew.
Comment #29
WilliamB CreditAttribution: WilliamB commentedThanks adf1969 !
I applied your patch, it works but i get an error message:
Notice : Undefined index: _weight in field_collection_field_widget_embed_validate() (line 979 in D:\projects\MySite\sites\all\modules\field_collection\field_collection.module).
It's because my content type has several collection fields:
- Paragraph which can have an unlimited number of value (repeatable)
- Prat which can have two values (repeated twice)
- Actuality which can only have one value (not repeated).
The Paragraphs and Prats values all have _weight attribut but Actuality doesn't since it's a single value field.
So you need to add a check, because otherwise you will get an error for single value collection field!
EDIT: Bellow i just edited your code adding the control:
Comment #30
achilles085 CreditAttribution: achilles085 commented#29 works for me..thanks @WilliamB
Comment #31
tim.plunkettAll of that is not necessary, there is already
field_sort_items()
which will take care of the sorting if the key is present.Also, this uses the constant
FIELD_CARDINALITY_UNLIMITED
instead of -1.Comment #32
szantog CreditAttribution: szantog commented#31 great patch!
After applied, the rearrange worked with embed widget
Comment #33
OnkelTem CreditAttribution: OnkelTem commentedHave just applied sucessfuly againt beta-2. Works fine, weights are saved.
Comment #34
tim.plunkettThanks everyone!
http://drupalcode.org/project/field_collection.git/commit/f534642
Comment #36
rogical CreditAttribution: rogical commentedUsers like me would like always using the release version than the dev version of the module.
Can you release a new beta version when some bugs are fixed? Especially, such a big improvement.
Comment #37
charlie-s CreditAttribution: charlie-s commentedI found dalin's module very usable. Might I suggest adding this function so that the admin theme is respected when sorting field_collection items:
Comment #38
charlie-s CreditAttribution: charlie-s commentedActually, it looks like the order that I'm setting is being ignored for items further down the list. Dalin, any ideas on this?Comment #39
p4trizio CreditAttribution: p4trizio commentedI applied manually the pacth at #31 and it works.