1

I've got a field template which is rendering a field collection field using the standard field.tpl.php file:

  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item): ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
     <?php endforeach; ?>
  </div>

I want to output the a CSS class inside the div in the loop with some text which comes from a colour field inside the $item variable.

EG. I would like to access the variable such as: render($item['field_colour']); in the loop, but this obviously doesn't work.

What's the best practice for doing this?

1 Answer 1

1

In standard field.tpl files you can access your field collection items using this code:

foreach ($items as $k) {
  foreach ($k['entity']['field_collection_item'] as $id => $field_collection) {
    $field_collection_item = field_collection_item_load($id);
    $field_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item);
    $field_colour = $field_wrapper->field_colour->value();
  }
}

I don't know if it is a best practice to do that, but this is my way.

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.