7

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

enter image description here

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

enter image description here

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 <img/> 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.

5
  • 1
    Did you consider using hook_field_attach_view_alter()?
    – avpaderno
    Commented Nov 29, 2012 at 13:46
  • Not terribly familiar with using that hook, but I didn't see how that would work.
    – mpdonadio
    Commented Nov 29, 2012 at 14:27
  • Its purpose is to alter the way a field is rendered, and it is invoked when field_attach_view(), or field_view_field() is called. Using that hook, you should be able to alter how the image field is rendered.
    – avpaderno
    Commented Nov 29, 2012 at 15:48
  • What happens when you also select Rendered file, in Enabled displays?
    – avpaderno
    Commented Nov 29, 2012 at 15:50
  • That display mode is an icon+link to the original file. The problem is more figuring out who calls the theme('image_style') for the options I outline, but I cannot figure this out. My next option is to make a MYTHEME_image_style so I can start looking at call stacks. Joy.
    – mpdonadio
    Commented Nov 29, 2012 at 17:09

2 Answers 2

1
+250

As you mentioned, Media renders fields via Drupal's field display formatter mechanism. Your custom module will work, but there is one for this: Linked Field

1
  • 1
    This does look like a better option, but it keeps blowing up on this particular site. I need to work this into one of my testing sites and see how it goes.
    – mpdonadio
    Commented Nov 29, 2012 at 19:42
4

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(&$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->filesize) {
    return;
  }

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

  if (file_entity_file_is_local($file) && $image = image_load($file->uri)) {
    if (!empty($display['settings']['image_style'])) {
      $element = array(
        '#theme' => 'image_style',
        '#style_name' => $display['settings']['image_style'],
        '#path' => $file->uri,
        '#width' => $image->info['width'],
        '#height' => $image->info['height'],
      );
      if ($display['settings']['image_style'] == 'MYSTYLE') {
        $element = array(
          '#theme' => 'link',
          '#text' => drupal_render($element),
          '#path' => 'media/' . $file->fid,
          '#options' => array(
            'attributes' => array(),
            'html' => TRUE,
          ),
        );
      }
      else {
      }
    }
    else {
      $element = array(
        '#theme' => 'image',
        '#path' => $file->uri,
        '#width' => $image->info['width'],
        '#height' => $image->info['height'],
      );
    }
    return $element;
  }
}
1
  • In the project I'm working on loading image on each call using image_load causes huge performance problems. If you look into default implementation of file_entity_file_formatter_file_image_view, you'll see that you could get image sizes with $file->image_dimensions array by width and height keys.
    – iamweird
    Commented Aug 22, 2014 at 9:57

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.