2

In Drupal 6 I used to make every sort of customised markup for CCK fields with multiple values. Usually this happened when I wanted to lay out thumnails for a gallery inside a table: I used to go into the relevant tpl.php file, grab the relevant $field_fieldname variable, loop through it, and print out the 'view' key for it, which contained the styled thumbnail, often linked to the original image and whatever.

Now, I'm a bit confused on how this should be done in Drupal 7. For now, I set up a suboptimal solution that goes like

<section class="gallery">
    <div class="row first">
    <?php foreach($content['field_gallery'] as $k => $v) : ?>
        <?php if(is_numeric($k)) : ?>
            <?php echo render($v) ?>
            <?php if((($k+1) % 4) == 0) : ?>
    </div><div class="row">
            <?php endif ?>
        <?php endif ?>
    <?php endforeach ?>
    </div>
</section>

but it's obviously wrong. I can't seem to get my head around this, even after having read lots of docs about the Field API and theming for D7. Ideas?

1 Answer 1

2

You've got an explanation on how field_view_field and rendering api for fields work in this blog.

Here you've got an example (extracted from the link above):

$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[$delta]);

You just need to loop over the $delta value for the fields to render them in a multiple valued field.

3
  • Yes, I've seen that blog post, and it seemed the most likely approach, though I think it's a bit out of context. I mean, in a template you should try not to call API functions, and this, unless done in some preprocess function, requires you to write a bit more code than it's necessary in the template itself. I was hoping in a more D6-ish solution, like at most calling render() on some render array.
    – Morpheu5
    Commented Nov 13, 2011 at 20:48
  • 1
    Anyway, this seems to be the most flexible approach I've seen so far, so I'm accepting the answer :) Thanks a lot.
    – Morpheu5
    Commented Nov 13, 2011 at 20:57
  • Ok, question: how do I configure it to work with Colorbox (and/or other lightbox plugins)? Using your third line, I get all the original images, not the thumbnails (and I've properly configure display settings in the Content Type settings). Using a display settings array things seem to get better, but I can't find what to use as image_link to make Colorbox pop up. Ideas?
    – Morpheu5
    Commented Nov 13, 2011 at 22:12

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.