1

I want to contain my custom content type's image field (cck) within a block. I define the block within hook_block_view:

function foxyidx_block_view($delta)
{
    $t = get_t();
    if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
        $node = node_load(arg(1));
    }
    switch($delta)
    {
        case 'foxyidx_images':
            $block['subject'] = '';
            $render_out = drupal_render($node->field_image);
            $block['content'] = <<<HTML
                {$render_out}
HTML;
            break;

If I were to render the image_field in a template I would use

print drupal_render($content['image_field']);

However, in the hook_block_view function I assume I need to use another function. Any ideas?

3 Answers 3

2

I think a combination of field_get_items() and field_view_value() would do the trick:

$image_items = field_get_items('node', $node, 'field_image');
$image_view = field_view_value('node', $node, 'field_image', array_shift($image_items));

$block['content'] = render($image_view);
2
  • You rock like a hurricane! Commented May 24, 2012 at 23:42
  • I modified this solution a little to handle multiple images in the image_field. $block['subject'] = ''; $render_out = drupal_render($node->field_image); $image_items = field_get_items('node', $node, 'field_image'); $image_view = ''; foreach($image_items as $image_item) { $image_view .= render(field_view_value('node', $node, 'field_image', $image_item)); } $block['content'] = $image_view; Commented May 25, 2012 at 2:24
2

I modified this solution a little to handle multiple images in the image_field.

$block['subject'] = '';
            $render_out = drupal_render($node->field_image);
            $image_items = field_get_items('node', $node, 'field_image');
            $image_view = '';
            foreach($image_items as $image_item)
            {
                $image_view .= render(field_view_value('node', $node, 'field_image', $image_item));
            }
$block['content'] = $image_view;
0

I found this answer on drupal.org It handles it in a different way.

http://drupal.org/node/154066

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.