Wednesday, July 27, 2011

Drupal 7 Theming and the image Path

In Drupal 7 there is a filefield in core now, there is also something like imagecache in Drupal 6 called image styles.

When it comes to theming you will soon find out that the image data stored in the node array is completely different than before. For example there is no filepath available.

Display image fields in templates

First and recommended solution would be to display the field as it is with the render() function:



But for doing some more customization (e.g. for a special image slider) you might need the path to the image.
This is not as easy as in Drupal 6, because the path is not available in the node object any more. So one handy solution is to use image_style_url() to get the path:

field_image['und'][0]['uri']);
?>

This is already the path to the formatted image. So you can display the image with theme_image() now.

theme_image()

In addition the theme_image function to output images has changed too:

path_to_theme() . '/images/picture.gif'));
?>


Notice that the variables have to be specified in a single array now. You can read more about this at http://api.drupal.org/api/function/theme_image/7.

Some people are calling this bad coding style, I don't like it also. ;)

Also working (but not tested):

$node->field_image['und'][0]['uri']));
?>


theme_image_formatter()

Another solution is the theme_image_formatter function, that does some of the stuff for you:

$node->field_image['und'][0],
'image_style' => 'image_style_name'
);

$output = theme_image_formatter($var);
?>

http://www.zites.net/en/drupal-7-theming-and-image-path

No comments:

Post a Comment