Repeatable: Always

Steps to repeat:
0. Dependencies on modules: Plupload (currently using 7.x-1.7), Multiple forms (currently using 7.x-1.1)
1. Adding a field with the type "Image" and the widget "Media browser"
2. Set "Number of values" at least to 2
3. Adding N (N > 2) images into this field

Expected Results:
- Upload in media browser popin:
bar.jpg (fid: 21)
baz.jpg (fid: 22)
foo.jpg (fid: 23)

- Files order after submit, in field data result (sorry for my bad English):
bar.jpg (fid: 21)
baz.jpg (fid: 22)
foo.jpg (fid: 23)

Actual Results:
- Upload in media browser popin:
bar.jpg (fid: 21)
baz.jpg (fid: 22)
foo.jpg (fid: 23)

- Files order after submit, in field data result:
foo.jpg (fid: 23)
baz.jpg (fid: 22)
bar.jpg (fid: 21)

I don't really know if this behaviour can be assimilated to a bug or a misunderstanding from me.
So I created a feature request and I will explain my use case.

When I add several images to upload, they are added in alphabetically order in the admin popin.
I want to keep this order.
After few tests I realized that the order is preserved on file_save (fid keep same order than filename).
But the files order in the field data result wrapper is loss. More specifically, the order is reverted.

I found out that its come from the re-indexing deltas in the function media_field_widget_submit, from file media.fields.inc

  // Re-index deltas after removing empty items.
  $submitted_values = array_values($submitted_values);

Is it possible to configure this behaviour from the field UI form ? (Checkbox allowing DESC or ASC order for the uploaded files)

Comments

nikolino’s picture

I've the same problem.
Have you find a solution?
Thanks

anfor’s picture

Yes I found a solution, here the patch I used.

However I didn't test the last Media release (7.x-2.0-beta1).

anfor’s picture

Status: Active » Needs review
scottalan’s picture

StatusFileSize
new575 bytes

Uploading a new patch with a different take. Needed this for our use case.

With this you can then do something like:

function MODULE_media_field_widget_submitted_values_alter(&$values) {
  uasort($values, 'MODULE_media_sort');
}

// This is from drupal_sort_weight() but using 'fid' instead.
function MODULE_media_sort($a, $b) {
  $a_weight = (is_array($a) && isset($a['fid'])) ? $a['fid'] : 0;
  $b_weight = (is_array($b) && isset($b['fid'])) ? $b['fid'] : 0;
  if ($a_weight == $b_weight) {
    return 0;
  }
  return ($a_weight < $b_weight) ? -1 : 1;
}
andrej galuf’s picture

I actually prefer the solution in #4, as it allows for more complex sorting solutions.

Works as advertised.

joseph.olstad’s picture

patch #4 still applies cleanly, would have to add some documentation if we added it.

Looks simple enough, just thinking that something like this should be documented somewhere, so included with the patch (say a patch to the Readme.txt or a new book page to the media documentation here: say under recipes (suggested location for this type of documentation))

joseph.olstad’s picture

This will likely get some attention soon.
First In First Out /first displayed/ first in the list.
seems most logical for usability.

  • anfor authored b90233b on 7.x-2.x
    Issue #2474639 by anfor: Loss upload order after submitting several...
joseph.olstad’s picture

Version: 7.x-2.0-alpha4 » 7.x-2.x-dev
Status: Needs review » Fixed

Thanks @anfor, works like a charm.

scottalan’s picture

StatusFileSize
new1.03 KB

Ended up needing to meet different sorting requirements. Wanted to post my solution here since this is where I posted the first patch I submitted.

We had a request that newly uploaded images get added to the top of the list vs the bottom of the list. In order to do this I needed to add a different alter hook as I didn't have all the information needed to determine what files were new and what files existed previously.

I've added a comment above the hook to better explain.

joseph.olstad’s picture

@scottalan , great stuff, can we please open a new issue for your patch? That way this one can stay 'fixed'
just add a link to the new issue like [#N0DEID]

Status: Fixed » Closed (fixed)

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