1

I have a view which shows a list of nodes of two different node types. Both have an image field.

Now I would like to have the image style to be different for the two node types.

I can't use hook_views_pre_render(), because then I would change the image style for all the nodes displayed. I also think I can't use a tpl.php file, because then I already have a URL, and can't retrieve another image style URL based on that that URL. Another option would be to define the image field twice in the view, and hide one depending on the node type, but I wouldn't know how to do that.

I'm using Drupal 7.

Any idea I overlooked?

6 Answers 6

2

Here is a better way to format your image or check out this post http://drupal.org/node/958726

$hero_image = array(
  'style_name' => 'case_study_teaser',
  'path' => $image['uri'],
  'width' => '',
  'height' => '',
  'alt' => $image['alt'],
  'title' => $image['title'],
  );
print theme('image_style',$hero_image);
1

At http://your_drupal_site/admin/config/media/image-styles You can add an image style. enter image description here

When you create the view configure the field under the image style choose the style you created.

enter image description here

3
  • I already have 2 different image styles defined. One will scale and one will scale and crop. I would like to use the first one when a node of node type 1 is shown, and the other one when it's a node type 2.
    – Jeroen
    Commented Dec 28, 2011 at 20:03
  • Are both the node types part of same view or these are two views.
    – 15dk51
    Commented Dec 29, 2011 at 5:24
  • It's one view which shows both the node types in one list (OR).
    – Jeroen
    Commented Dec 30, 2011 at 15:26
1

I found a way. A bit hacky I guess, but it does the trick:

In "views-view-field--viewname--viewpage--field-img.tpl.php" put the following code:

if ($row->node_type == 'nodetype_with_text') {
  print $output;
}
else {
  $url = image_style_url('imagestyle_for_nodetype_without_text', $row->field_field_img[0]['raw']['uri']);
  $output = preg_replace('/src=".*" *width=".*" *height=".*"/', 'src="'.$url.'"', $output);
  print $output;
}

The width and height of the image style could be retrieved, but I think it isn't worth the hassle, the images are resized anyway.

1
  • That is really hacky
    – chrisjlee
    Commented Jan 31, 2012 at 2:07
0

You can use this module,

Views Conditional,

Suppose your image field is called field_my_image.

Add this image two times AND exclude them from display.

->the first instance will display the field with an image style A

->the second instance will display the field with an image style B

Add content->type as a field AND exclude from display.

While in the view add "Views conditional field".

Add a condition:

if content-type: A{

  output First instance of field_my_image

}else{

  output Second instance of field_my_image

}

-1

I would load the field as raw URL, and then load the image style according to the node type. I do believe this is the right way, if no module provides this out of the box.

-2

If you only have a few images and the file size is small you could resize them with css. Not the most elegant solution but a quick one ;-)

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.