3

I have a problem with theming field collection. I want to create a loop and I have a field called field_earth inside field_helloworld that displays unlimited images. The following code doesn't show any image.

<?php
  $counter = 1;
  foreach ($node->field_helloworld['und'] as $key => $value) {
    $field_collection = entity_load('field_collection_item', array($value['value']));
    $idx = $value['value'];
?>
  <p>
    <small><?php print $counter; ?></small>
    <?php print $field_collection[$idx]->field_venus['und'][0]['value'];?>
  </p>
  <div>
    <?php print $field_collection[$idx]->field_earth['und']['0'][value];?>
  </div>
  <?php $counter++; ?>
<?php  } ?>

Can anyone show me how to display all images?

4 Answers 4

3
/**
 * @Show all field collection values from node object
 *  Example field collection name: "field_helloworld"
 *  Example fields within a field collection:
 *  - field_title       (string)
 *  - field_image       (image)
 *  - field_description (string)
 */


// get field collections from node 
$field_helloworld = field_get_items('node', $node, 'field_helloworld');

// get all fields values if collection exists
if (!empty($field_helloworld)){
    $field = field_view_value('node',$node, 'field_helloworld', $field_helloworld[0]);  
    foreach ($field['entity']['field_collection_item'] as $id => $field_collection){

        // load the field collection item entity
        $field_collection_item = field_collection_item_load($id);
        // wrap the entity and make it easier to get the values of fields
        $field_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item);

        // all values from a field collection
        $field_title        = $field_wrapper->field_title->value();
        $field_image        = $field_wrapper->field_image->value(); // an array of image data
        $field_description  = $field_wrapper->field_description->value(); 

        // an example of getting image url from field_image
        $image_url          = $field_image['uri'] ? file_create_url($field_image['uri']) : '';
    }
}

Notice that there is no ['und']['0'][value].

2

I have a node with a field collection called "field_table." I used field_collection_item_load() to get all the values inside it. You can get all URIs inside an array, and loop to display the images.

 $node = node_load(71); // This is the node containing the field collection.
 $arr = array();

 foreach ($node->field_table['und'] as $oneRow) {
   $row = field_collection_item_load($oneRow['value']);
   $arr[] = array(
     'image' => $row->field_img['und'][0]['uri']
   );
 }
0

The title of this question is Show all values for a field collection in node.tpl.php...

Add this to your node.tpl.php file to dump all the output:

  print '<pre>' . check_plain(print_r($content, 1)) . '</pre>';
0
<img src="<?php print image_style_url("thumbnail", $field_collection[$id]->field_image['und'][0]['uri']);?>"> 

Use image_style_url and it will work. field_image is the machine name for image field.

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.