I intended to post a feature request to ask how to make a custom formatter to be able to show a single field from a collection within a view.
It turns out that this is already possible, yay.


Create a view to list your content type.
Add a relationship to the collection.

Save the view & now you can select the 'Field-collection items' in the Add fields dialog. Select the fields & add them to the view. You can tweak the display in the usual views ways :)

I'm sure the developers already know how to do this, but I couldn't find any reference to it anywhere.

Thank you, does this need adding to the readme or am I doing something horribly wrong?

Comments

drew reece’s picture

StatusFileSize
new738 bytes

Here is a small patch that explains that field collections can be used in views.
I really don't know if it is appropriate to mention other non-required modules but I think it needs noting somewhere.

drew reece’s picture

I'm not sure I understand how to get the single field values from field collections that allow multiple values via views, my initial suggestion fails to get anything but the first value of a repeating field.
The field formatter doesn't have the usual option to deal with multiple items.

I gave up & used EVA to attach a view that shows all the values for a single field within the collection.

Is selecting single fields from a collection (with multiple items) actually possible ?

robcarr’s picture

Status: Active » Reviewed & tested by the community

@drew reece thanks for posting the tip. Relationships gives me exactly what I needed but hadn't twigged it at all - thumbs up for your patch to readme.txt as it was the prompt I needed.

Will do more head scratching and see what fun I can have now with Views. I'm in the world of Panels on latest project, so using contextual filters rather than EVA.

Set to RTBC as your note/patch at #1361076-1: Display single value from collection within view. is very helpful.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, veiws-note-1361076-2.patch, failed testing.

drew reece’s picture

StatusFileSize
new1.04 KB

Lets see if a git format patch works.

drew reece’s picture

Status: Needs work » Needs review

Status change & please bear in mind repeating fields don't seem to work.

Status: Needs review » Needs work

The last submitted patch, veiws-note-1361076-5.patch, failed testing.

drew reece’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev

I patched against 7.x-1.x, now will it apply?

drew reece’s picture

Status: Needs work » Needs review

#5: veiws-note-1361076-5.patch queued for re-testing.

emptyvoid’s picture

I'm beginning to think the best course of action is to use the relation module and fore go using the field collection and references modules. But of course, that doesn't help me with finishing a project I am currently working on as I'm too far along to start over. :( boohoo for me.

emptyvoid’s picture

I have accomplished displaying one or more "rows" of the field collection for a parent node.
Use case:
I wanted to display a list of projects where I built a field group that defined a status log. Such as date:12/01/2012 status:Started, etc. Over the course of the project the "log" would change status.

It involved the following:

Modules
field collection
views_php

I created a view package called projects_index.
The first display is a page view displaying a list of projects and select fields I want. There is no way to display the field collection "fields" in this view so don't even try.

Next I created a block view called "project status block".
In the block I start out filtering the view by project (the parent) and for debugging I display the title of the project node. Next I go to relations and select the field group field where multiple "log" entries are saved.

From there I select the field collection "fields" I want to display, if you did it right you should see a group of fields under the field collection type in the field browser.

I setup the block view to display as a table for each field. I added a "nid" field for the project, then added an argument that expects a "nid" (this is the parent project nid). To test it get the nid of a parent node and pass it to the view preview to make sure the field collection records display.

(save the view package) This is so the block view will be available for the page view.

Now go back to the page view and add an additional field, select a field from the "global" field group and use the php field. From within the field call views_embed_view('project_index', 'block_1', $row->nid);

This should call the block view and pass the current nid of the project in each row. It will render the field collection entries for each project (parent).

If you want to limit the records returned for the block you can set the block display to not be a paging interface and limit it to 1 or 3 or whatever limit you require.

ikeigenwijs’s picture

Issue summary: View changes

I also used the php view approach.

The problem i have nested field collections and the relations doe not travel down deeper than the first level.
so i used php to get the id of the collection field from
the row load the entity get the next id of the deeper level collection field the deepest feeld i need is of type Location

Not the neatest solution, all tips are welcome

//get first field collection praktijklocatie
$field_collection = entity_load('field_collection_item', array($row->field_adres_acc_col));
//dsm($field_collection);
//dsm($row->item_id);
//dsm($field_collection[$row->item_id]->field_praktijk_locatie_acc['und'][0]);
//get field collection item adres from field collection praktijk locatie
$field_collection_adres= entity_load('field_collection_item', array($field_collection[$row->item_id]->field_praktijk_locatie_acc['und'][0]['value']));
//dsm($field_collection_adres);
return $field_collection_adres[$field_collection[$row->item_id]->field_praktijk_locatie_acc['und'][0]['value']]->field_adres_acc_col['und'][0]['longitude'];
giupenni’s picture

Following the instructions of the patch it works fine.

chris matthews’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The 7 year old patch in #5 does not apply to the latest field_collection 7.x-1.x-dev and if still relevant needs a reroll.

Checking patch README.txt...
error: while searching for:
  * Widgets for embedding the form for creating field collections in the
    host-entity can be provided by any module. In future the field collection
    module might provide such widgets itself too.
    

Restrictions
-------------

error: patch failed: README.txt:26
error: README.txt: patch does not apply