Wrapping a rendered file from Media with a link This is a question related to How to set Media Styles in Media 7.1.2 / Media 7.2.X?.

I am using Media 1.2 in a Drupal 7 site to add some fields to images (caption, credit, etc). 

If I have an image field set up to display as a Rendered file with a particular view mode, eg



I have my display set up the way I want to show the proper fields for this view mode.  In the file display, eg



is there a way to wrap the image in a link to the media/fid page?  I am not seeing how to do this from the UI, and Theme Developer isn't giving me any useful info for which theme functions and preprocess/process functions are being invoked between the one for the whole image field (as far as the content type is concerned) and the image itself (the actual &lt;img/&gt; element).

At this point, I do not want to switch to Field Collection, but would entertain switching to Media 2.x if that is my only option.
OK, here is the working code.  

The Media module implements hook_file_formatter_info() to set up a callback for rendering images.  

As is the Drupal way there is a hook_file_formatter_info_alter() to change the default behavior.  So, I took the default callback from file_entity.module and cloned it into a custom module.  I then wired this up with the alter.  My custom callback has some additional logic to check for an image style, and then wrap the element with a link.  

The only real quirk is that the default implementation of theme_link() assumes it is a leaf element, so you need to render out the image element first.

As a side note, this is also how you would get alt/title text onto the images, which is missing by default.

function MYMODULE_file_formatter_info_alter(&amp;$info) {
  if (!empty($info['file_image'])) {
    $info['file_image']['view callback'] = 'MYMODULE_file_formatter_file_image_view';
  }
}

function MYMODULE_file_formatter_file_image_view($file, $display, $langcode) {
  // Prevent PHP notices when trying to read empty files.
  // @see http://drupal.org/node/681042
  if (!$file-&gt;filesize) {
    return;
  }

  // Do not bother proceeding if this file does not have an image mime type.
  if (strpos($file-&gt;filemime, 'image/') !== 0) {
    return;
  }

  if (file_entity_file_is_local($file) &amp;&amp; $image = image_load($file-&gt;uri)) {
    if (!empty($display['settings']['image_style'])) {
      $element = array(
        '#theme' =&gt; 'image_style',
        '#style_name' =&gt; $display['settings']['image_style'],
        '#path' =&gt; $file-&gt;uri,
        '#width' =&gt; $image-&gt;info['width'],
        '#height' =&gt; $image-&gt;info['height'],
      );
      if ($display['settings']['image_style'] == 'MYSTYLE') {
        $element = array(
          '#theme' =&gt; 'link',
          '#text' =&gt; drupal_render($element),
          '#path' =&gt; 'media/' . $file-&gt;fid,
          '#options' =&gt; array(
            'attributes' =&gt; array(),
            'html' =&gt; TRUE,
          ),
        );
      }
      else {
      }
    }
    else {
      $element = array(
        '#theme' =&gt; 'image',
        '#path' =&gt; $file-&gt;uri,
        '#width' =&gt; $image-&gt;info['width'],
        '#height' =&gt; $image-&gt;info['height'],
      );
    }
    return $element;
  }
}