Getting the source URL of image in Media Gallery Module I need one more thing to get my custom lightbox running in the Media Gallery module for Drupal 7.

The link that is clicked which leads to a custom page which contains the image is at this moment the following:

l($image, $link_path, array('html' =&gt; TRUE, 'attributes' =&gt; $attributes)) : $image; 


The thing is that I want to link directly to the image, and not to a page where the image is on! I know that this is hard to understand so I'll try to clarify. At this moment, when you click the link you will be directed to something like this:
/media-gallery/detail/xxx/xxxxx

But I want it to link to the image itself (e.g. /files/folder.1.JPG) and I can't seem to find where the image is transformed into a page on itself. Does anyone have ANY idea? I would be soooo grateful!

Thanks a lot you guys.
EDIT: I have found that the generated link can be edited by the following snippet of code in colorbox-display.js: 

var $ = jQuery, $galleries, $gallery, href, $links, $link, $dummyLinksPre, $dummyLinksPost, i, j;
    // Add a colorbox group for each media gallery field on the page.
    $galleries = $('.field-name-media-gallery-media');
    for (i = 0; i &lt; $galleries.length; i++) {
      $gallery = $($galleries[i]);
      $links = $('a.sbEnabled', $gallery);
      $dummyLinksPre = $gallery.parent().find('ul:has(a.shadowbox-supplemental-link.pre)');
      $dummyLinksPost = $gallery.parent().find('ul:has(a.shadowbox-supplemental-link.post)');
      $dummyLinksPost.appendTo($gallery);
      $links = $links.add('a', $dummyLinksPre).add('a', $dummyLinksPost);
      $links.attr('rel', 'shadowbox-' + i);
      for (j = 0; j &lt; $links.length; j++) {
        // Change the link href to point to the lightbox version of the media.
        $link = $($links[j]);
        href = $link.attr('href');
        $link.attr('href', href.replace(/\/detail\/([0-9]+)\/([0-9]+)/, '/lightbox/$1/$2'));
      }
}


Unfortunately this doesn't bother to work.
The link you're looking to override is hard coded into the template_preprocess_media_gallery_media_item_thumbnail() function in the Media Gallery module. 

Fortunately, as it's a preprocess hook for a theme function, you can yourself override it in your site's theme. It does mean having to duplicate some code from the original preprocess function, and also another theme function so you can add the rel attribute, but it's not too much. You'll also need to implement hook_js_alter() to remove the colorbox js the the Media Gallery module adds by default.

Place the following code in your theme's template.php file and replace all occurrences of THEMENAME with your theme's name:

function THEMENAME_preprocess_media_gallery_media_item_thumbnail(&amp;$variables) {
  $element = $variables['element'];

  // Only make the changes for images, no other media type.
  if (strtolower($element['#bundle']) != 'image') {
    return;
  }

  $format = $element['#media_gallery_entity']-&gt;media_gallery_format[LANGUAGE_NONE][0]['value'];
  $lightbox = is_numeric(strpos($format, 'lightbox')) ? TRUE : FALSE;

  /*
   I'M PRESUMING HERE THAT YOU'RE ADDING THE SHADOWBOX SCRIPT USING ANOTHER 
   METHOD (PERHAPS IN THE THEME'S .info FILE). IF THAT'S NOT THE CASE JUST 
   UNCOMMENT THE FOLLOWING LINE AND CHANGE THE PATH TO ADD IT HERE

   if ($lightbox) {
     $element['file']['#attached']['js'][] = drupal_get_path('theme', 'THEMENAME') . '/path/to/shadowbox.js';
   }
  */ 

  // Get the rendered file without annoying DIV wrappers.
  $element['file'] = array('#theme' =&gt; 'media_gallery_file_field_inline', '0' =&gt; $element['file']);
  $image = drupal_render($element['file']);

  $gallery_id = $element['#media_gallery_entity']-&gt;nid;
  $media_id = $element['#file']-&gt;fid;

  // Add a class that is a more targeted version of what template_preprocess()
  // automatically adds for this theme hook, to enable per-type (e.g., video vs.
  // image) styling.
  $variables['classes_array'][] = drupal_html_class('media_gallery_media_item_thumbnail_' . $element['#file']-&gt;type);

  // Add a class for the wrapper.
  $variables['classes_array'][] = 'media-gallery-item-wrapper';

  // Create an array of variables to be added to the main thumbnail link.
  $link_vars = array();
  $link_vars['image'] = $image;


  // THIS IS WHERE YOU MAKE THE CHANGE TO THE LINK PATH. YOU MENTIONED THAT
  // YOU'VE ALREADY MANAGED TO ADD THE 'rel' ATTRIBUTE THAT YOU NEED SO I WON'T
  // ADD THAT HERE
  // Old code: $link_vars['link_path'] = "media-gallery/detail/$gallery_id/$media_id";

  // New code:
  $style_name = 'media_gallery_large'; // Name of the image style for the full sized image you want to link to
  $url = image_style_url($style_name, $element['#file']-&gt;uri);
  $link_vars['link_path'] = $url;
  $link_vars['rel'] = 'shadowbox'; // The rel attribute
  // End new code


  $link_vars['classes'] = array('media-gallery-thumb');
  $link_vars['title'] = $element['#bundle'] == 'image' ? t('View larger image') : t('Watch video');
  // Add the image as a link to the detailed view
  $variables['media_gallery_item'] = theme('media_gallery_item', $link_vars);

  // Set the variables to theme the metadata.
  $meta_vars = array();
  $meta_vars['location'] = $element['#media_gallery_entity']-&gt;media_gallery_image_info_where[LANGUAGE_NONE][0]['value'];
  $meta_vars['title'] = isset($element['media_title']) ? $element['media_title'][0]['#markup'] : '';
  $meta_vars['link_path'] = $link_vars['link_path'];

  // Theme the metadata.
  $variables['media_gallery_meta'] = theme('media_gallery_meta', $meta_vars);
}

function THEMENAME_media_gallery_item($variables) {
  $image = $variables['image'];
  $link_path = $variables['link_path'];
  $attributes = array();
  if (!empty($variables['classes'])) {
    $attributes['class'] = $variables['classes'];
  }
  if (!empty($variables['title'])) {
    $new_image = str_replace(array('title=""', 'alt=""'), array('', ''), $image);
    $image = str_replace('/&gt;', ' title="'.$variables['title'].'" alt="'.$variables['title'].'" /&gt;', $new_image);;
  }

  // Below is the only change to this function, just to add the rel attribute.
  if (!empty($variables['rel'])) {
    $attributes['rel'] = 'shadowbox';
  }

  // Add sliding door top div and wrappers
  $item = '&lt;div class="media-gallery-item"&gt;&lt;div class="top"&gt;&lt;div class="top-inset-1"&gt;&lt;div class="top-inset-2"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="gallery-thumb-outer"&gt;&lt;div class="gallery-thumb-inner"&gt;';
  // Create a link around the image
  $item .= empty($variables['no_link']) ? l($image, $link_path, array('html' =&gt; TRUE, 'attributes' =&gt; $attributes)) : $image;
  // Add sliding door bottom div and close wrappers
  $item .= '&lt;/div&gt;&lt;/div&gt;&lt;div class="bottom"&gt;&lt;div class="bottom-inset-1"&gt;&lt;div class="bottom-inset-2"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
  return $item;
}

function THEMENAME_js_alter(&amp;$js) {
  // Remove the media gallery colorbox js. It interferes with the rel attribute.
  if (isset($js['sites/all/modules/media_gallery/colorbox-display.js'])) {
    unset($js['sites/all/modules/media_gallery/colorbox-display.js']);
  }
}


I've added some comments to the code to explain what's happening so it might be worth giving the code a quick glance over.

After you've cleared Drupal's caches you should get exactly what you're after :)