I have a field collection containing an image+caption pair attached to the article content type. You can add one or many to an article. The articles are split into pages using the smart_paging module. I would like to be able to display the first image+caption pair on the first page, 2nd pair on the 2nd page, 3rd on the 3rd, etc.

Do you think the best way to implement this through a custom field formatter? Is there an easier way to configure this behavior through the UI?

Thanks for any ideas....

Comments

donpwinston’s picture

I've been trying to do something similar. I have a field collection with image, caption, and credits fields. The only way I can control the output is to create a template for the content type it is used in. Using the field template technique is not feasible for me because all you get to work with is the $content variable. You need the actual cck field arrays. A new formatter is also not practical or very complicated. You might be able to get a field template to do what you want. Create a template file with the name field-collection-item-{field_name}.tpl.php. I do not know where or how you can get the appropriate "page number" from here with just the $content variable.

So to do what you want create a image-caption.inc file that prints the appropriate image and caption on the desired page. There should be some way to determine the page to assign the appropriate value to a variable like $page_no from the $node variable in the template. Then do something like the following: (Mine is a little more complicated then necessary for you. My code creates a photo gallery for a field collection field)

<?php
	$tw_photos = array();
	$photo_ids = array();
	if (isset($field_photo_gallery))
		foreach ($field_photo_gallery as $photo_id)
			if (isset($photo_id['value'])) $photo_ids[] = $photo_id['value'];
	$entity_items = entity_load('field_collection_item', $photo_ids);
	foreach ($entity_items as $item) $tw_photos[] = $item;
	if (empty($tw_preset_main)) $tw_preset_main = 'tw_normal';
	if (empty($tw_preset_minor)) $tw_preset_minor = 'tw_thumbnail';
	if (empty($tw_high_res)) $tw_high_res = FALSE;
     
        $page_no = ???????????;
?>
<?php if ($page_no == 1) { ?>

<div class="tw-photo-gallery">

<div class="normal-image">
	<?php if (isset($tw_photos[0]->field_photo['und'][0]['uri'])) { ?>
	<div id="tw-normal-image-0">
	<?php print theme('image_style', array('style_name' => $tw_preset_main, 'path' => $tw_photos[0]->field_photo['und'][0]['uri'])); ?>
	<div class="image-description">
		<div class="caption"><?php if (isset($tw_photos[0]->field_caption['und'][0]['value']))
				print $tw_photos[0]->field_caption['und'][0]['value']; ?></div>
		<div class="credits"><?php if (isset($tw_photos[0]->field_credits['und'][0]['value']))
				print $tw_photos[0]->field_credits['und'][0]['value']; ?></div>
		</div>
	</div>
....blah blah
<?php }  else if ($page_no  == 2) { ?>

....
<?php } ?>

in your node content type template hide your field collection field and include the above.

donpwinston’s picture

Excuse me. I messed some things up. The Drupal documentation is frgging maddening. The template file name should be field--field-photo-gallery.tpl.php (copy from module/field directory) or you might be able to use field-collection-item--field-photo-gallery.tpl.php. (Copy this and rename to your field from the field_collection module directory)

The comments inside these template files provided by Drupal and the field_collection module are wrong about the naming conventions used.

jmuzz’s picture

Issue summary: View changes
Status: Active » Fixed

Theme support for field collection is an ongoing issue. See #1157794: Move markup to template files and improve theming in new 2.x branch.

A custom formatter seems like a good option. You can use the custom formatters module to add a formatter using the UI, but you'll still be using PHP code. Similarly you can use display suite and create a custom field, probably a code field.

Status: Fixed » Closed (fixed)

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