Currently, the sort criteria of the view output is:

"Field collection item: Field collection item ID (asc)"

This differs from the order set in the node edit form.

It would be good if the output conforms to the order set in the node edit form.

Great project BTW.

Comments

bennybobw’s picture

This is what I did in my hook_views_pre_render to fix this:

//order field collection view correctly
  if ($view->name == 'field_collection_view') {
    $args = explode('+', $view->args[0]);
    if (!empty($args)) {
      $order_results = $view->result;
      $aflip = array_flip($args);
      usort($order_results, function($a, $b) use($aflip) {
        if (isset($aflip[$a->item_id]) && isset($aflip[$b->item_id])) {
          return $aflip[$a->item_id] > $aflip[$b->item_id] ? 1 : -1;
        }
        return 0;
      });

      $view->result = $order_results;
    }
  }
rosberg’s picture

I have been struggling with this issue myself.
Thank you very much bennybobw for a working solution!

daniel-san’s picture

Issue summary: View changes

Just wanted to let you know that this worked for me as well. Thank you for the solution.

neerajskydiver’s picture

The solution works well with the ordering. However I tried making a generic solution for the problem by fetching the delta of the items in the field collection but that did not work out as the weight is not saved in the database.
Unless field collection module itself saves some kind of information about the weight this is the best solution to sort out the issue.
Cheers

dieppon’s picture

Hi,

hook_views_pre_render did not work for me but MY_MODULE_views_post_execute did.

Kind regards