How to modify image field in field_attach_preprocess_alter I have an image field called field_grid_image on a custom node type "event". I want to populate this field when not available but I don't want to use a default image.
I want to add a default image based on the node's taxonomy. I have written the following code to load the image from node's taxonomy field but it doesn't show up in the final output.

function custom_event_field_attach_preprocess_alter(&amp;$variables, $context) {
    if($context['entity_type'] == "node" &amp;&amp; $context['entity']-&gt;type == "event" &amp;&amp; sizeof($context['entity']-&gt;field_grid_image) == 0){
        $tid = $context['entity']-&gt;field_event_type['und'][0]['tid'];
        $taxonomy_term = taxonomy_term_load($tid);

        $values = field_get_items('taxonomy_term', $taxonomy_term, 'field_generic_image');
        $variables['field_grid_image']['und'][0] = $values[0];

        dpm ($values);
        dpm( $variables);
    }
}


When I print $variables I see the field populated from the taxonomy as follows.



Any ideas why the image is not rendered in the final output?

Thanks,
I've figured out the problem. I should have used field_view_value to render the field and add it to $variables['content'] instead of editing the field directly.

The below code works as expected now.

function custom_event_field_attach_preprocess_alter(&amp;$variables, $context) {
    if($context['entity_type'] == "node" &amp;&amp; $context['entity']-&gt;type == "event" &amp;&amp; sizeof($context['entity']-&gt;field_grid_image) == 0){
        $tid = $context['entity']-&gt;field_event_type['und'][0]['tid'];
        $taxonomy_term = taxonomy_term_load($tid);

        $values = field_get_items('taxonomy_term', $taxonomy_term, 'field_generic_image');
        $node = $variables['node'];
        $output = field_view_value('node', $node, 'field_grid_image', $values[0], array(
            'type' =&gt; 'image',
            'settings' =&gt; array(
            'image_style' =&gt; 'gridimage',
            'image_link' =&gt; 'content',
            ),
        ));

        $variables['content']['group_image_text']['field_grid_image'] = $output;
    }
}