i think, i have a typical use case and i don't bring it to a working state (as a designer) ...

fundament:
- node template for the content type the field collection is used
- i don't want to use field templates (i already removed all surrounding tags in my basic field.tpl)
- i want to print the node fields directly into the node.tpl - no general content render
- i have to get rid of all needless wrapper stuff generated by field collection (clearfixes etc.) - i want a clean markup (hack of the module would be ok)
- including dealing empty fields
- i would pay money for it (tell me the amount you need) + i will post the solution here

structure:
- content type template: node--nodeblock.tpl.php
- field collection: field_coll_img_link (unlimited)
- 2 sub fields: field_coll_img_single (single value) + field_coll_link_single (single value)
- the image field display, set in the render settings, is only the raw scaled image - for example:
<img alt="" src="http://www.foo.com/sites/default/files/styles/thumb-sidebar/public/foo.png" />
- the link field display, set in the render settings, is only the raw url - for example:
http://foo.com/

task:
- let the link surround the image - for example:
<a href="http://foo.com/"><img alt="" src="http://www.foo.com/sites/default/files/styles/thumb-sidebar/public/foo.png" /></a>

it would be wonderful if somebody could solve this for me with normal $content values or entity API wrappers ...

thanks a lot + best regards sb

Comments

guybedford’s picture

I had this same issue - managed to solve it with the following php code in the template:

In my case, the name of the field collection is: "field_tabs"
This field collection contains two fields: "field_tab_name" and "field_tab_content"

The php is included below, which is included in "node--node-type.tpl.php".

<?php
    $field_collection = entity_load('field_collection_item', array($node->field_tabs['und'][0]['value']));
    print $field_collection[1]->field_tab_name['und'][0]['value'];
    print $field_collection[1]->field_tab_content['und'][0]['value'];
?>

For many field collection values, the $field_collection array can be looped for the different groups. This is in the case of one value.

Note also the above code includes no error checking, language checking, and ideally you should place this in the node preprocessor!

Morasta’s picture

Similar issue as OP and #1. In my case I had an unlimited number of values. Here's a modification that let me loop through each instance and print them out.

In this example I needed two fields, date and description, together on a single row. Left the divs in to show structure for those who need it.

<?php
foreach ($node->field_schedule['und'] as $key => $value) {
  $field_collection = entity_load('field_collection_item', array($value['value']));
  $idx = $value['value'];
?>
  <div class="my-row">
    <div class="my-datetime">
      <?php  print $field_collection[$idx]->field_datetime['und'][0]['value'];?>
    </div>
    <div class="my-descrip">
      <?php print $field_collection[$idx]->field_description['und'][0]['value']; ?>
    </div>
  </div> <!-- my-row -->
<?php 
} ?>
morybel’s picture

This is exactly what I needed !! Thanks!!!! Here's what I did to include it in html (for formating purposes). No errors, datas gets displayed correctly in my table.

<table  border="1" cellpadding="1" cellspacing="1" style="width:900px;">
<tr>
	<td style="width:100px;"><strong>Article</strong></td>
	<td style="width:20px;"><strong>Justification</strong></td>
	<td style="width:70px;"><strong>Quantit&#233;</strong></td>
	<td style="width:50px;"><strong>Prix unitaire</strong></td>
	<td style="width:50px;"><strong>Taxes</strong></td>
</tr>			
<?php 
foreach ($node->field_description['und'] as $key => $value) { $field_collection = entity_load('field_collection_item', array($value['value'])); $idx = $value['value']; 
?> 
<tr>
	<td><?php print $field_collection[$idx]->field_article['und'][0]['value'];?></td>
	<td><?php print $field_collection[$idx]->field_justification['und'][0]['value']; ?></td>
	<td><?php print $field_collection[$idx]->field_quantite['und'][0]['value']; ?></td>
	<td><?php print $field_collection[$idx]->field_prix['und'][0]['value']; ?></td>
	<td><?php print $field_collection[$idx]->field_taxes['und'][0]['value']; ?></td>
<?php 	
} ?>	
</tr>
</table>

However, whenever I hit F5 for refreshing I keep having these errors:
Notice : Undefined index: 8 in include() (ligne 81 in /sites/mysite/theme/node--annexe_a.tpl.php).
Notice : Trying to get property of non-object in include() (ligne 81 in /sites/mysite/theme/node--annexe_a.tpl.php).

Line 81, line 82, line 83 etc... an error for each header in my table.
I clear my caches, they disappears. Then I hit F5 and here they are again.

I'm not a programmer, so I would love for anyone to point me to my error.
Thanks for everything.

EDIT:

I answered my problem by myself. I had a default value on my "quantity" that causes to add a extra blank line at the end of my table. Deleting the default value made the errors disappears. Just in case someone have the same problem
morybel’s picture

Would any of you know how to make a numbered list with this? ($count ?)

Thanks

jorlowsk’s picture

Ok, so I have this half working which is a lot further than where I was three weeks ago. I have a field collection named "field_project". In that I have two fields named "filed_projectphoto" which is an image and "field_projectphotocaption" which is text. Field "field_projectphotocaption" prints to screen just fine, however, (tested with a couple of different image fields) I can't get an image to print to screen.

<?php
foreach ($node->field_project['und'] as $key => $value) {
  $field_collection = entity_load('field_collection_item', array($value['value']));
  $idx = $value['value'];
?>
	<div style="float:left"><?php print $field_collection[$idx]->field_projectphoto['und'][0]['value'];?></div>
	<div style="float:left"><?php print $field_collection[$idx]->field_projectphotocaption['und'][0]['value']; ?></div>
<?php 
} ?>

In turn I am also getting this error:
Notice: Undefined index: value in include() (line 6 of /home/whitepoi/public_html/dev.followvue.com/sites/all/themes/VUE/node--projects.tpl.php).
Notice: Undefined index: value in include() (line 6 of /home/whitepoi/public_html/dev.followvue.com/sites/all/themes/VUE/node--projects.tpl.php).

Any help would be appreciated. Thanks in advance.

- Jeff

morybel’s picture

Hi Jorlowsk,
I also had this error notice. It turned out I had a default value on one of my fields that caused to add a extra blank line at the end of my table. Deleting the default value (and the extra blank line) made all the errors disappears.

For your image, try this:

<?php print $field_collection[$idx]->field_projectphoto['und'][0]['uri'];?>

Hope this help.

quasi’s picture

This would be the code adapted for the counter:

<?php
    $counter = 1;
	foreach ($node->field_schedule['und'] as $key => $value) {
	  $field_collection = entity_load('field_collection_item', array($value['value']));
	  $idx = $value['value'];
?>
    
      <div class="my-row">
        <h2><?php print $counter; ?>. Step</h2>
        <div class="my-datetime">
          <?php  print $field_collection[$idx]->field_description['und'][0]['value'];?>
        </div>
      </div>
  <?php $counter++; ?>
<?php  } ?>
morybel’s picture

Thank you soooooooo much... as of flexifield in D6 I had to put [$count] in every array. That was my mistake. I'm very greatfull for your help.
:-)

sachbearbeiter’s picture

thanks to all posters ...

Blue’s picture

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.
Problem is that : print $field_collection[$idx]->field_earth['und']['0']['value']; it does not display any image.
If i print print $field_collection[$idx]->field_earth['und']['0']['uri']; I do get values (public://logo.png) but I don't get the links to the images.

Someone can show me how can I display all images?

Many many thanks with this.

<?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  } ?>
morybel’s picture

You should check here:
http://robotlikehuman.com/web/printing-cck-content-field-values-drupal-7

This whole discussion is about templating a node with drupal 7 content field. Very instructive. There is a comment about how to print images from a field.
Hope this helps.

jeffglenn’s picture

I have been looking over this and other posts for quite sometime and I still can't seem to figure this out...

In D6 I was able to easily node_load(); in a Multi-Group and create a foreach loop to load the data I needed from each field. Here's the code I have in D6:

<?php $slideshow = node_load('9'); ?>				
  <ul class="fader">			
    <?php foreach ($slideshow->field_homepage_slides_title as $delta => $field) { ?>
    <li style="background:url(<?php print $slideshow->field_homepage_slide_image[$delta]['filepath']; ?>) no-repeat center center;">
      <div class="wrap clearfix">
        <div class="slide-message">
          <h2><?php print $slideshow->field_homepage_slides_title[$delta]['value']; ?></h2>
          <p><?php print $slideshow->field_homepage_slide_message[$delta]['value']; ?></p>
        </div>
      </div>
    </li>
  <?php } ?>			
</ul>

Now I'm trying to do the same thing with Field Collections. I figured I could use the entity_load(); like this:
<?php $field_collection = entity_load('field_collection_item', array($item_id)); ?>
then krumo(); that variable and pull the information needed. However as you all probably already know that doesn't work. I've tried quite a few different combinations and none seem to work.

Any help at all would be appreciated

jeffglenn’s picture

For anyone interested I thought I would post a follow-up. I got it working with this code. Basically the same principle just using Field Collection Instead of MultiGroups.

<?php $slideshow = node_load('8'); ?>		
  <ul>		
    <?php foreach ($slideshow->field_slide['und'] as $key => $value) {
      $slide = entity_load('field_collection_item', array($value['value']));
      $idx = $value['value'];
    ?>		
    <li>			
      <div class="slide-message">
        <h2><?php print $slide[$idx]->field_slide_title['und']['0']['value']; ?></h2>
        <p><?php print $slide[$idx]->field_slide_message['und']['0']['value']; ?></p>
      </div>
      <img src="<?php print image_style_url('homepage_slider', $slide[$idx]->field_slide_image['und']['0']['uri']); ?>" />
    </li>		
    <?php } ?>		
  </ul>
jeffglenn’s picture

Issue summary: View changes

field_coll_img_single (single value) + field_coll_link_single (single value)
more clear now

jmuzz’s picture

Status: Active » Closed (duplicate)

Theming around field collection is still a work in progress. See #1157794: Move markup to template files and improve theming in new 2.x branch.

It sounds like you guys have found workarounds that will work in your situations so I will close this.