I have a view with a thumbnail of the image uploaded by user. I would like to setup a default image which appears in place of thumbnail in case the user did not upload any image. Is that possible? Any advice appreciated.
3 Answers
In a view, you can set the "empty field behavior, so you could put a hard coded image in there if one isn't present, or, in the content type itself, you can set up a default image if one isn't set for that field.
-
-
What version of views are you using? The empty text field here sez: "Provide text to display if this field returns no results. You may include HTML. You may enter data from this view as per the "Replacement patterns" in the "Rewrite Results" section below." Apr 18, 2012 at 16:53
-
-
I'm running Views 3. Don't have a copy of 2 running anywhere to see what's what back then :( Apr 18, 2012 at 17:10
-
1Regardless, unless you need this thumbnail specifically different in the view, setting the default image inside the content type works just fine. Apr 18, 2012 at 17:18
This can be done when you edit the content type > your image field > fieldsettings. There you can upload a default image. This image should show up in views.
-
The only thing is that the default image will not only appear as a thumbnail but also in the full node view, which is undesirable.– VonderApr 18, 2012 at 17:28
I tried numerous things to solve this problem for Drupal 7: Display a default icon on the teaser, but not the full view. In the end I came up with this solution:
- Edit the field on the content type and set your placeholder image as the default image for the field.
- Now the placeholder image shows up for teasers without thumbnails. But it also shows up on the full node view, which we don't want.
- In my theme's template.php I implemented template_preprocess_node(). Inside this hook I check the node's type to see if it's one of the ones I want to change. Then I check the view mode to make sure it's set to 'full'. Then lastly I check the contents of the field and see if it contains the default image. If so then I hide the field using the Drupal hide() function.
It took a bit of trial and error but here is my working code. I am looking at three different content types, my field was named "field_hero_shot" and my placeholder image was named "placeholder.png". You will of course have to adjust these to match your content type and field names.
function mytheme_preprocess_node(&$variables) {
// For Press room news types, hide the default thumbnail placeholder
// image in the full display mode.
// turn on for debugging: dpm($variables);
if (isset($variables['type']) &&
( ($variables['type'] == 'company_update')
|| ($variables['type'] == 'media_clipping')
|| ($variables['type'] == 'news_release')
) &&
isset($variables['view_mode']) && ($variables['view_mode'] == 'full')) {
// then it's a press room type and it is in full display mode!
// See if the Hero Shot field contains "placeholder.png".
if (isset($variables['field_hero_shot'][0]['filename'])
&& ($variables['field_hero_shot'][0]['filename'] == 'placeholder.png')) {
// If so that's the default image and we should hide it!
hide($variables['content']['field_hero_shot'][0]);
}
}
}