Tuesday, September 18, 2018

Drupal 7: How to theme field collections

Let's say we have a field collection named "field_link_headers" (check the structure of the content type). And we want to theme a field inside the field collection called "field_link_image":

Step 1: copy field-collection-item.tpl.php file present in the Field Collection module directory and paste it into your theme's template folder

Step 2: rename the file field-collection-item.tpl.php to field-collection-item--field-link-headers.tpl.php with only <?php print render($content); ?> in it.

Step 3: make a duplicate of field-collection-item--field-link-headers.tpl.php and rename it to field--field-link-image--field-link-headers.tpl.php

Step 4: to render the field contents, add the following code to the above mentioned file
 
//render image field contents
foreach ($items as $delta => $item):
print render($item);
endforeach;
Step 5: clear drupal caches

No comments:

Post a Comment