0

I've used field_view_field to display an image field for a specific entity in Drupal. The problem is when that entity doesn't have any value for field. I expect field_view_field to display the default image for field but it doesn't load default value of the field.

How can I do this?

4
  • can you post the code you have that isn't working properly?
    – rooby
    Commented Jan 21, 2015 at 8:40
  • I would start debugging from image_field_prepare_view() where it handles default images.
    – rooby
    Commented Jan 21, 2015 at 23:31
  • My code was: $user_pic = drupal_render( field_view_field('user', $user_loaded, 'field_profile_photo', array( 'label' => 'hidden', 'settings' => array( 'image_style' => '170_170' ) ))); Commented Jan 22, 2015 at 5:20
  • Can you post it in your question instead of the comment? It's easier to read.
    – rooby
    Commented Jan 22, 2015 at 5:26

4 Answers 4

1

field_view_field is a correct function to use if you want to display node image (if provided) or default (attached to field settings) if not.

$node = node_load($nid);
print render(field_view_field('node', $node, 'field_image')));
2
  • According to the question they are already doing this but it isn't showing the default image when it should be.
    – rooby
    Commented Jan 21, 2015 at 8:41
  • 1
    I know and I'm surprised. Even checked if it works on local copy of D7 and everything seems to be ok. @Sebto - are you sure that default image is correctly loaded into field settings form and correctly configured in Manage Display -> Default? Try to dpm field_view_field for random node with image and other one without image (with default image uploaded).
    – zaporylie
    Commented Jan 21, 2015 at 22:23
0

In our case we have two set of images

  1. Default image
  2. another is node specific image

i created two block in views in the first view get the default image it will always have the default image

and in the second block render the image if the image field is empty render the whole default block there, below is the sample code

in views block 2 select the image field in the view and exclude it from display

in views global php add the code below

<?php
    $banner = isset($data->field_field_banner)? $data->field_field_banner : '';
    if(!empty($banner)){
      print drupal_render($banner);
    }else{
          print render_view_block_for_noresult('view_name','block_name');
    }
?>


/* function to render default view blocks if no banner is present */
function render_view_block_for_noresult($view_name='',$block_name=''){
        $view = views_get_view($view_name);
        if(isset($view) && !empty($view))
            return $view->preview($block_name);
}

this will help you to print the image and get the default image printed in the view.

1
  • 2 views to display one image seems like overkill. Also you should be able to do this without custom code by putting one view in the empty text setting of the first view so that if the first view returns no results the view in the empty text displays.
    – rooby
    Commented Jan 21, 2015 at 8:39
0

By the way, I find a solution for my problem. If there aren't any value for field, it retrieves field instance information then load default image of field and display it.

       $user_loaded = user_load($uid);
if ($user_loaded->field_profile_photo[LANGUAGE_NONE][0]) {
  $user_pic = drupal_render(
    field_view_field('user', $user_loaded, 'field_profile_photo', 
      array(
        'label' => 'hidden',
        'settings' => array(
          'image_style' => '170_170'
        )
      )));
}
else {
  $instance = field_info_instance('user', 'field_profile_photo','user');
  $user_loaded->field_profile_photo[LANGUAGE_NONE][0] = (array)file_load($instance['settings']['default_image']);
  $user_pic = drupal_render(
    field_view_field('user', $user_loaded, 'field_profile_photo', 
      array(
        'label' => 'hidden',
        'settings' => array(
          'image_style' => '170_170'
        )
      )));
}
0

Here's an example of how to use a field instance's default image as a fallback when the individual image field itself is empty.

To briefly explain the concept, use field_get_items() to attempt to get the image attached to the entity (e.g. node, user, taxonomy_term, ...), then fallback to field_info_instance() if an image has not been uploaded.

Once an image is available to work with, set the $file variable to the image reference, which will get rendered after calling field_view_value().

$field_image = field_get_items('node', $node, 'field_image');

if (!empty($field_image)) {
  $file = $field_image[0];
}
else {
  $info = field_info_instance('node', 'field_image', 'blog');

  if (!empty($info) && $info['settings']['default_image'] > 0) {
    $default_image_fid = $info['settings']['default_image'];

    $file = (array) file_load($default_image_fid);
  }
}

if ($file) {
  $image = field_view_value('node', $node, 'field_image', $file, array(
    'type' => 'image',
    'settings' => array(
      'image_style' => NULL,
    ),
  ));

  print render($image);
}

All of this logic could be wrapped up into a helper function or simply used wherever needed.

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.