Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Expounding on some basics you can enjoy. I did this in my /mytheme/template.php
Even if you do not use any modals such as Colorbox, the examples might help you adapt the code to meet your needs. I "flowered" up the output a bit to demo that you can tune the stuff to appear as needed:
function YOUR_THEME_NAME_file_link($variables) {
$file = $variables['file'];
$icon_directory = $variables['icon_directory'];
$url = file_create_url($file->uri);
$icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options = array(
'attributes' => array(
'type' => $file->filemime . '; length=' . $file->filesize,
),
);
// Use the description as the link text if available.
if (empty($file->description)) {
$link_text = $file->filename;
}
else {
$link_text = $file->description;
// $options['attributes']['title'] = check_plain($file->filename);
$options['attributes']['title'] = check_plain($file->description . ' (' . $file->filemime . ')');
}
// open files of mime types from the array below in colorbox or a new window
// colorbox etc
$new_window_mimetypes0 = array('application/javascript',
'text/plain',
'text/css'
);
$new_window_mimetypes1 = array('image/gif',
'image/jpeg',
'image/png'
);
// end colorbox etc
$new_window_mimetypes2 = array('application/json',
'application/pdf',
'application/vnd.lotus-wordpro',
'application/x-msaccess',
'application/mp4',
'application/rss+xml',
'application/rtf',
'application/vnd.google-earth.kml+xml',
'application/vnd.google-earth.kmz',
'application/vnd.rn-realmedia',
'application/vnd.solent.sdkm+xml',
'application/vnd.wordperfect',
'application/wsdl+xml',
'application/x-font-ttf',
'application/x-7z-compressed',
'audio/x-pn-realaudio',
'application/x-tar',
'application/x-rar-compressed',
'application/x-shockwave-flash',
'application/xslt+xml',
'application/xspf+xml',
'application/x-stuffit',
'application/x-stuffitx',
'application/xhtml+xml',
'application/xml',
'application/zip',
'audio/basic',
'audio/midi',
'audio/mp4',
'audio/ogg',
'audio/webm',
'audio/x-mpegurl',
'audio/x-ms-wma',
'image/svg+xml',
'image/tiff',
'image/vnd.adobe.photoshop',
'image/x-icon',
'text/calendar',
'text/html',
'text/csv',
'text/sgml',
'video/x-flv',
'video/3gpp',
'video/mp4',
'video/mpeg',
'video/ogg',
'video/quicktime',
'text/vnd.wap.wml',
'text/vnd.wap.wmlscript',
'video/webm',
'video/x-m4v',
'video/x-msvideo',
'video/x-ms-asf',
'video/x-ms-wm',
'video/x-ms-wmv',
'video/x-sgi-movie',
'application/msword',
'application/vnd.visio',
'application/x-mspublisher',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
'application/x-msdownload');
if (in_array($file->filemime, $new_window_mimetypes0)) {
$options['attributes']['class'] = 'colorbox-800';
$options['attributes']['rel'] = 'geshi-filter';
// return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . ' [Modal Colorbox] (' . $file->filemime . ')' . '</span>';
}
if (in_array($file->filemime, $new_window_mimetypes1)) {
$options['attributes']['class'] = 'colorbox';
$options['attributes']['rel'] = 'nofollow';
// return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . ' [Modal Colorbox] (' . $file->filemime . ')' . '</span>';
}
else if (in_array($file->filemime, $new_window_mimetypes2)) {
$options['attributes']['target'] = '_blank';
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . ' (' . $file->filemime . ')' . '</span>';
}
}
And jQuery function for the colorbox-800 class (resides in mytheme.js or whatever method you use for custom javascript functions).
(function($) {
$(document).ready(function() {
$(".colorbox-800").colorbox({
iframe:true,
fastIframe:false,
innerWidth:'100%',
innerHeight:'100%',
transition:"elastic",
opacity:.7,
onComplete:function(){
$.fn.colorbox.resize({width:'90%', height:'90%'});
},
});
});
})(jQuery);
*note the class "colorbox" is Native to Colorbox and functions as long as "Colorbox" is installed.
Comments
Comment #1
DRIVE CreditAttribution: DRIVE commentedDrupal is changing the markup specified in the PHP CODE BLock-=- eg: the two uncommented lines were added by Drupal when I posted the code. I tried several times but apparently a bug wants to add extra markup :)
Comment #2
DRIVE CreditAttribution: DRIVE commentedps - using on Drupal 7
Comment #3
quicksketchThanks for posting this @DRIVE. While useful as a reference, we probably won't be adding anything like this to the main project. You might consider adding this a handbook page in the documentation instead: http://drupal.org/documentation/modules/filefield