4

I've got a Field Collection on use in a basic page content type on my Drupal 7 site. I've built a view with the Data Export module that is outputting the data from nodes of this content type as a CSV. The view is configured to dump all of the Field Collection data for each parent node into one cell... not ideal, but I'll live with it.

The problem is that there is no separator between the fields in the Field Collection. So, the contents of the Field Collection cell in the CSV looks like this:

Fruit_1: AppleFruit_2: OrangeFruit_3: Banana

It makes reading through the data in that cell unbearable (the separator should be after Apple and Orange... oh and fruit is just for this example, not actual data).

How do I get some kind of separator in there? Doesn't have to be anything fancy; something as simple as a comma and a space would be lovely! Ex:

Fruit_1: Apple, Fruit_2: Orange, Fruit_3: Banana

I've experimented with Field Collection Views, but no luck.

3
  • On data export settings page you will find the separator option where you can add the separator.or another solution is you can use views tpl files which are used to format the fields and apply the css for that particular field collection field. Commented May 7, 2014 at 14:28
  • @AkshaySawant Thanks. The separator is used in between multiple Field Collections. Sadly, it has no effect on the fields within the Field Collection. I'll explore the tpl files, although since I'm exporting to CSV, I'm not sure how modifying CSS will help.
    – Sam
    Commented May 7, 2014 at 14:40
  • Yes I tried Field Collection Views and Views Data Export and no success with it as well.
    – pal4life
    Commented May 8, 2015 at 16:11

2 Answers 2

2

The solution is to implement a views_pre_render hook in a custom module.

function mymodule_views_pre_render(&$view) {
    if ($view->name == 'myview') {
        foreach ($view->result as $key => $row) {
            $line = array();
            foreach ($view->result[$key]->field_field_fruit_collection as $n => $collection) {
                $item = current($collection['rendered']['entity']['field_collection_item']);
                $line[] = $item['field_fruit'][0]['#markup'] .  ' (' . $item['field_color'][0]['#markup'] . ')';
                unset($view->result[$key]->field_field_fruit_collection[$n]);
            }
            $view->result[$key]->field_field_fruit_collection[0]['rendered']['#markup'] = implode(', ', $line);
        }
    }
}

In this example, the collection field is field_fruit_collection which is made up of two fields: field_fruit and field_color. The contents of the exported collection field will look like :

"Apple (red), Banana (yellow), Kiwi (green)"
1

Try using the view's Relationships settings, set your field collection as relationship and you should be able to select its fields and proceed from there just as if they were your node's (or whatever entity's) native fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.