I have a field collection with the number set to Unlimited such that I can "Add More" of them while editing. The weights seem to be totally ignored in how they are displayed when using this module. The weights are saved just fine (I can see on the edit page where the ordering is correct) and other display methods (e.g. Table, Embedded) also display the fields in the correct order. On the other hand, this module ignores the weights and displays the field collections strictly in the order they were created. I'm not sure if there's some setting in the View I'm using that would allow for this to work based on the weights. I tried a variety of sorting options in the view but nothing worked so far.

Comments

piwchix’s picture

I have excactly same problem. Tried to agregate, unagregate JS, disable enable, different modules.

It works with simple CCK fields, but doesn't work with multiple field collection as CKK field.

g089h515r806’s picture

I have test with this situation:
1,i have a resume profile type which have a work experience field collection field.
2, in work experience field collection, there are some other field, such as" company name", "duration",all of the is single value.

3,when i change duration from single value to multi value,

4,the views does not work correctly as my expectation.

Is this the same issue compared with yours?

THis is not a bug of this module, this is a bug of field collection or entity API. it already exist when i start build this module.

g089h515r806’s picture

This module is an addon for E-recruiter installation profile, I have test it on this profile. Multiple Value issue already exist when i build this module.

thirdboxcar’s picture

I'm not sure this is an issue of the original Field Collection module...when I make a normal View on the field (unlimited field collection) they order correctly, but this View has no ability to sort by the top level delta. I can access all the child Field Collection deltas through your View, which is great, but not the top level.

Marco G.’s picture

I have the same needs, and it's not possible to access the Field Collection delta of top level field, but only the Filed Collection Item Id, which is the value used to order the View as default.

So I tried to add the delta top level field, making some changes to the module files. Now I can display the delata value as a view field and it works fine.
I can also add the field as sorting and filtering field, but when I do this I receive the following error in the view result:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_collection_item.delta' in 'field list' (where delta is the name I gived to the field)

Following the change I've done to modules file:

1) created "field_collection_views_handler_field_delta.inc" as follows:

class field_collection_views_handler_field_delta extends views_handler_field {
function query() {
// Do nothing, as this handler does not need to do anything to the query itself.
}

function option_definition() {
$options = parent::option_definition();
return $options;
}

function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
}
/**
* Work out the ITEM ID Delta
*/
function render($values) {
$field_path = "";
$item_id = $values->item_id;
//debug($values);
$field_collection_item = field_collection_item_load($item_id);
// $delta = field_collection_field_get_path($field_collection_item->delta());
$delta = $field_collection_item->delta();

return $delta;
}
}

2) added in "field_collection_views.info" the line to include
files[] = views/field_collection_views_handler_field_delta.inc

3) added in "field_collection_views.views.inc" the following function:
$data['field_collection_item']['delta'] = array(
'title' => t('Item id - Delta'),
'help' => t('The delta value of Item ID.'),
'field' => array(
'handler' => 'field_collection_views_handler_field_delta',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);

I'm not so skilled on drupal, php, ecc. but it seems I'm at a step from the solution...has anyone an idea about how to complete this work??

css117’s picture

Hello,
I would be very interested by this function too.
It looks native, instead of using draggable view : the weight info and the admin section work allready fine thanks to field collection implementation.

We "just" need to have the weight field accessible in the sorting criteria.
Btw the delta isn't available :/

It would be sad to have to add a new integer field to handle it manually...

Thanks in advance !

Watergate’s picture

Status: Active » Closed (works as designed)

I don't think this is a shortcoming of the Field Collection Views module. In the field's formatter the arguments are provided in the order of the delta (i.e., weight) value (to View's contextual filter). Although I haven't looked in much detail in the way Views generates the SQL query, it seems that the standard way is using the plain IN operator (which, logically, doesn't look at the way the parameters are ordered).

However, to make the ordering respect the top level delta values, I have 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. This works in all my use cases, hopefully it does for you too :)

joelcollinsdc’s picture

@#7 i couldn't get this to work... what are you sorting by?

Watergate’s picture

I don't have anything in the sort criteria.

The view generating the field collection item, has the mentioned required relationship (see #7). (BTW, don't forget to check 'Allow multiple values' when configuring the contextual filter)

In the top level view, I use the formatter provided by the Field Collection View module and checked the multi-field settings to display the results in the same row.

Synerverse’s picture

@#7 Bingo!!!!! That's the ticket! Thank you MrWatergate for finding that!

shashank7’s picture

#7 worked great. Thanks MrWatergate.

bpadaria’s picture

Issue summary: View changes

#7 worked for me too. Thanks MrWatergate.

mrP’s picture

@MrWatergate -- superb! thanks a ton for #7

sigkg’s picture

Hi.

The grouping for mine isn't working on the 3rd level.

This is with Delta 2
1. Item 1
1.1
1.2
1.2.1
1.2.2

With Delta All:
1. Item 1
1.1
1.2
1.1.1
1. Item 1
1.1
1.2
1.2.1
1.2.2

It appears it doesn't know that 1.1.1 belongs under 1.1. Can it because this was typed in after everything else was.

Thanks

sigkg’s picture

Forgot to mention:

The sub-sub-item (1.2.1) is on Delta 2 with the Field collection item: Sub-item (1.1) Without the relationship the sub-sub-item doesn't appear.

code-drupal’s picture

The ordering can be achieved for field collection view in a custom ways by altering the views query

function custom_views_query_alter(&$view, &$query)
{	

/* Field Collection Order as field collection does not order based sorting*/
if ($view->name == 'view name')
{
	if($view->current_display == 'default')
	{
		$fids = str_replace('+',',',$view->args[0]);	
		$view->query->orderby[0]['field'] = "FIELD(field_collection_item.item_id, ".$fids.")";
	}
} 
}

The above function will order it as per the requirement based on the field collection ids passed.

code-drupal’s picture

The ordering can be achieved for field collection view in a custom ways by altering the views query

function custom_views_query_alter(&$view, &$query)
{	

/* Field Collection Order as field collection does not order based sorting*/
if ($view->name == 'view name')
{
	if($view->current_display == 'default')
	{
		$fids = str_replace('+',',',$view->args[0]);	
		$view->query->orderby[0]['field'] = "FIELD(field_collection_item.item_id, ".$fids.")";
	}
} 
}

The above function will order it as per the requirement based on the field collection ids passed.

Anonymous’s picture

Tried this but nothing happends.
What's the point in being able to move items when they're not being saved?

Anonymous’s picture

How to solve this issue?

mareks’s picture

I only needed to 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.

bpadaria’s picture

Thanks MrWatergate.

Comment #7 worked for me as well.

mikemccaffrey’s picture

Version: 7.x-1.0-beta2 » 7.x-1.0-beta3
Status: Closed (works as designed) » Active

This is still an issue. There needs to be a sort criteria for making field collection items display in the proper order.

Even after following the instructions in #7 there is nothing in the query that would ensure that items are sorted properly by the delta field:

SELECT field_collection_item.item_id AS item_id
FROM 
{field_collection_item} field_collection_item
INNER JOIN {field_data_field_photobank} field_data_field_photobank ON field_collection_item.item_id = field_data_field_photobank.field_photobank_value
INNER JOIN {node} field_photobank_field_collection_item ON field_data_field_photobank.entity_id = field_photobank_field_collection_item.nid
WHERE (( (field_photobank_field_collection_item.nid = '103456' ) )AND(( (field_collection_item.field_name IN  ('field_photobank')) )))
mikemccaffrey’s picture

Here is the query if I take the same view as above and add a sort criteria on the delta of the field instance in a related node:

SELECT field_collection_item.item_id AS item_id, field_photobank_field_collection_item__field_data_field_photobank.delta AS field_photobank_field_collection_item__field_data_field_phot
FROM 
{field_collection_item} field_collection_item
INNER JOIN {field_data_field_photobank} field_data_field_photobank ON field_collection_item.item_id = field_data_field_photobank.field_photobank_value
INNER JOIN {node} field_photobank_field_collection_item ON field_data_field_photobank.entity_id = field_photobank_field_collection_item.nid
LEFT JOIN {field_data_field_photobank} field_photobank_field_collection_item__field_data_field_photobank ON field_photobank_field_collection_item.nid = field_photobank_field_collection_item__field_data_field_photobank.entity_id AND (field_photobank_field_collection_item__field_data_field_photobank.entity_type = 'node' AND field_photobank_field_collection_item__field_data_field_photobank.deleted = '0')
WHERE (( (field_photobank_field_collection_item.nid = '103456' ) )AND(( (field_collection_item.field_name IN  ('field_photobank')) )))
ORDER BY field_photobank_field_collection_item__field_data_field_phot ASC

The problem is that when adding the sort condition, it assumes that the view type is for nodes, and not the field collection, so it goes to merge the field collection onto the nodes that have been already merged onto the field collections. This means that if you have 5 records, you get 25 results, as each record is joined to every other one.

How can we make a sort available for the delta on the field collection in its relationship to the node, rather than on the node's relationship to to the field collection field?

ron_s’s picture

@mikemccaffrey, I think you might be facing the issue I've seen quite a few times.

The problem has to do with the base table selected for building the View. When adding a new view, users are presented a "Show" dropdown selection that allows for an entity to be selected. This selection serves as the foundation for building the rest of the view.

If Field collection item is picked, there will be extremely unpredictable results. I've seen situations where random LEFT JOINs are added, result rows are duplicated many items, and sorting becomes impossible.

The correct approach that has worked for me every time is select the entity or node as the base table, and then build a connection to the field collections using the Views relationships. From there, the field:delta option can be used to sort the field collection information appropriately.

joshuautley’s picture

Ron, I was build a view with a Profile base table and used a relationship to the field collection based on your comment above (#24). That was the ticket to bring in the delta values so I could filter and sort. Thank you!!!

airb’s picture

#24: "field:delta" is field collection weight, thanks!

maxplus’s picture

Hi,

for me it worked to add the relationship to the host entity (node) and add a sorting criterium on the delta

Thanks!

Francewhoa’s picture

kellyimagined’s picture

Status: Active » Closed (works as designed)

#27 provides instructions on how to complete.

Eric At NRD’s picture

I could not get #7 to work right for my use case. However, an alternate method using hook_views_post_execute() DID work for me:
https://www.drupal.org/project/field_collection_views/issues/2075593#com...

I post this here only in case others ran into circumstances where #7 did not work.