How do I get the value of a field collection field in Views? I realize that http://drupal.org/node/1047784 is an ongoing views integration thread. With PHPCustom field print_r($data), I can see the value(s) of the field collection. How can I print just one of the fields in the collection?

Example:
Shorts (field collection in node)
>>Brand (a field in field collection Shorts)
>>Color (a field in field collection Shorts)

In views, I just want the values for Brand per each nid.

Comments

Title:Views PHPCustom Field - Get value of a field collection field Get value of a field collection field in Views or node.tpl

Not meaning to bump this; just adjusting the title to make it more accurate. I am also wondering how to get the individual fields print in node.tpl without using Views.

I wrote a little about this in #1107214: Field Collection tpl overrides and accessing field collection field values from node.tpl and #1155752: How to theme field collection?. The latter is more about embedded field collections, and gets a little confusing.

It's a little bit more involved than just printing things in the node.tpl. The short answer is you use entity_load() on your field collection value, then go one level down in the array with a foreach statement, then you have an entity object that contains your field collection fields that you can use just like the node object.

or you can use the entity API wrappers...

<?php
$wrapper
= entity_metadata_wrapper('node', $node);
// In case of a non-multiple collection and an example text field, you could get the text via
print $wrapper->field_collection->field_example->value();
// In case of a multiple collection and an example text field, you could get the text of the first one:
print $wrapper->field_collection[0]->field_example->value();
// and so one..
?>

Though, I'd suggest doing such kind of stuff in the template preprocessor...

Thanks fago, I didn't know about entity wrappers. They look simpler and more powerful -- and I'm guessing they're superior in performance as you can load them once and get the whole chain, as opposed to calling entity_load() multiple times. Any other benefits we should be aware of?

I'll give them a shot on my next project, and then contribute a node preprocess function and some theming docs back.

>Any other benefits we should be aware of?
hm, it automatically uses the right default field-language, so you don't have to care.

>I'll give them a shot on my next project, and then contribute a node preprocess function and some theming docs back.
Sounds great!

RobW and fago, thank you for the replies. It did answer my question. However, to the next step/level, I'm trying to have it also evaluated the min value of the fields.

For example:
Field Collection[0]
field_sample -- value = 2.00
field_example
Field Collection[1]
field_sample -- value = 3.00
field_example

I'd like to get an array of the field_sample(s) so that I can do a min(). It seems you have to do a foreach on the field collection and then field_sample (??). Is this possible with the wrapper - please explain? Unfortunately I'm not a programmer and I'm not successfully hacking it with my research and limited knowledge.

Edit: Figured it out finally; I was missing the field after the $i.

<?php
    $wrapper
= entity_metadata_wrapper('node', $node);
   
$spaces = array();
    foreach (
$wrapper->field_collection as $i){
         
$sizes[] = $i->field_sample->value();
    }
    
$spaces = implode( ",", $sizes);
?>

fago - the wrapper is very handy!

Status:Active» Fixed

#6 implies this is fixed.

Status:Fixed» Closed (fixed)

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

Status:Closed (fixed)» Active

Sorry to open this up again, but I'm a bit confused about how to retrieve one of the filed-collection fields in a view.

Do I need to add some sort of a custom php field to the view or is there a way to do this via the views UI.

Thanks ahead of time,

CarbonPig

i think some people (including me :) have problems how to print field values ...
maybe it would be helpful, if somebody would solve #1294536: Output in node--foo.tpl.php and we put it as a complete example (because it's so typical) without abstraction into the issues ...

@9 i'm sorry - i also have no solution for field based views ...

I get no results when putting into my node--content_type.tpl - is there anything I need to have additional before this statement

<?php $wrapper = entity_metadata_wrapper('node', $node);
print
$wrapper->my_field_collection->    field_collection_sample->value();
print
$wrapper->my_field_collection[0]->    field_collection_sample->value();
?>

For inclusion into the node template, I provided the code that works for me here: http://drupal.org/node/1294536#comment-5362592

I'm a bit confused on this working with the Views field template (views-views-field--myview--mycollection_field.tpl.php). The only variables available to this template are (as commented in views-views-field.tpl.php):

- $view: The view object
- $field: The field handler object that can process the input
- $row: The raw SQL result that can be used
- $output: The processed output that will normally be used.
When fetching output from the $row, this construct should be used:
$data = $row->{$field->field_alias}

Trying to render just a single field from within my field collection object, and don't seem to be able to traverse down the $row object array to grab the necessary value. Should entity_metadata_wrapper() be used to wrap a field collection object (couldn't get it to work for me)?

Any nudges greatly appreciated

With the dev module installed, you should be able to

<?php
dsm
($row)
?>
inside your views tpl to see the array. This thread is relating more to getting the results in a node.tpl (without a views query)

Not exactly elegant, but this seems to be a way to get values of a field collection item into a View (using the 'Field Collection Items' Formatter in your field for the View:

<?php
$collection
= $row->my_collection;
$titles = array();
if(isset(
$collection)){
  foreach(
$collection AS $element){
    if(isset(
$element['rendered']['entity']['field_collection_item'])){
      foreach(
$element['rendered']['entity']['field_collection_item'] AS $collection_item){
       
//dsm($collection_item); //if you need to examine your field collection
       
$item = $collection_item['my_collection_field']['#object'];
       
//grab necessary collection values:
       
$item_name = $item->my_collection_field['und']['0']['safe_value'];
       
$item_id = $item->item_id;
       
//etc...
       
$titles[] = $item_name . " " . $item_id;
      }
    }
  }
}
$list = implode( ", ", $titles);
print
$list;
?>

And this code went into a Views theme template (views-views-field--myview--mycollection_field.tpl.php) - which you can work out in your view in Advanced > Theme - Information.

If anyone has a better solution, please post. Probably could do with some more intuitive variable names. (*EDIT* slight tweak to code and added dsm() statement - commented out)

I hope this should answer #9 now, so maybe this issue can be closed?

In the view you can add a relationship to the field collection and than access all fields of this field collection thru this relationship. Where is the problem?

Status:Active» Closed (works as designed)

If you are comfortable with relationships, you can add a Field Collection relationship in a View to extract specific values. But the question raised was how to present a field of a Field Collection object using a tpl.php. I think.
Job done?

#12 worked for achieving this in the node template, node--foo.tpl.php. Added a post for handling unlimited values: http://drupal.org/node/1294536#comment-5644956

working code for print field collection items in views

<?php
$collection
= $row->_field_data['nid']['entity']->field_phones_group;
$titles = array();
if(isset(
$collection)){
    foreach(
$collection AS $element){
         
//dprint_r($element);
       
foreach($element AS $collection_item)  {
           
$field_collection = entity_load('field_collection_item', array($collection_item['value']));
           
//dprint_r($field_collection);
           
foreach($field_collection AS $item)  {
               
//dprint_r($item);
               
$type = taxonomy_term_load($item->field_phone_type['und'][0]['tid']);
                print
$type->name;
               
//dprint_r($type);
               
print $item->field_phone_phone['und'][0]['value'];
            }
        }
    }
}
?>

@wbue I am able to access specific fields by adding a relationship, but the problem is that it only prints that specific field in the FIRST field collection. I have multiple entries of the collections and I want to print a specific field out for each entry, not all of the fields.

So, i found out what my issue was... I was only displaying 1 item in the pager view settings. Duhh :(

Fago, your code snippet works like a charm for most field collection fields. Thanks!

I'm having trouble adapting it to images... fields that are images (ones that get uploaded from admin UI) display as arrays:
so for example:

<?php
  print_r
($i->field_thumbnail->value());
?>

results in:

Array ( [fid] => 46 [alt] => [title] => [width] => 116 [height] => 83 [uid] => 1 [filename] => Screen shot 2012-05-10 at 2.43.17 PM.png [uri] => public://Screen shot 2012-05-10 at 2.43.17 PM.png [filemime] => image/png [filesize] => 14662 [status] => 1 [timestamp] => 1336676039 [rdf_mapping] => Array ( ) )

Any suggestions to render the image instead?

Thanks!

Figured it out:

file_create_url($thumb['uri']) creates a web-accessible URL.... then just wrap it in an Only local images are allowed. tag.

to print a certain field of a field collection in a node.tpl.php do:

<?php
$wrapper
= entity_metadata_wrapper('node', $node);
foreach (
$wrapper->field_fieldcollectionname as $i): ?>

  <?php  print $i->field_myfield->value(); ?>
<?php endforeach; ?>
?>

benchi, it seems that there's a problem with your code in #25, can you help me to make this work ?

delete the last "?>"

<?php
  $wrapper
= entity_metadata_wrapper('node', $node);
  foreach (
$wrapper->field_fieldcollectionname as $i):
?>

  <?php print $i->field_myfield->value(); ?>
<?php endforeach; ?>

If it's in a tpl file, that fixes the syntax. Haven't tested if the code actually works though.

Working off of #20 is what got mine to work. Thanks so much!