Hi

I am using views 2 to create a list of download nodes with direct links to their attached files for download. iTweak work great for displaying the mime type icons when viewing a single node but does not seem to do anything when displaying attached files in views. Is there a way to do this using iTweak or do I have to create a custom view field template and have all the mime type icons in my theme?

If its in my theme I could do the following perhaps:

$ext = // get extension

if( file_exists(“./icons/”.$ext.”.png”) ){
$img = “./icons/”.$ext.”.png”;
} else {
$img = “./icons/unkown.png”;
}

print “”;

Thanks!

Comments

vincetingey’s picture

StatusFileSize
new28.08 KB

This is an screenshot of what it currently looks like. I'd prefer to display the icons to the left of the file name or just the icons.

vincetingey’s picture

Status: Active » Fixed

Well since no one replied I came up with a solution for myself without hacking this module! I created a custom views field template that parses the xhtml and replaces links with the appropriate image icon depending on the file extension. Here it is for everyone to enjoy!

Place this file in your template directory and name it using the views template theming conventions. This will vary depending on your view name. Mine was called "views-view-field--form-downloads--page--upload-fid.tpl.php"

Your custom icons will need to be in your theme directory or somewhere else.

 /**
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */

	// Parse the html output to extract the file locations and replace the text output with an icon based on its file extension.
	// IMPORTANT! This function has multiple dependancies:
	// 1) The file field must be linked in the view to the files themselves.
	// 2) The PHP DOM extension installed if using PHP 4, it is included in PHP 5 I believe.
	
	if($output){		
	
		/** The image root directory **/
		$image_dir = "/sites/all/themes/msl/images/content/mime_types/";

		/** a new document object model object **/
		$dom = new domDocument;
	
		/** load the html into the object **/
		$dom->loadHTML($output);
	
		/** discard white space **/
		$dom->preserveWhiteSpace = false;
	
		/** Gather all the filenames because they are links **/
		$links = $dom->getElementsByTagName('a'); 
		
		/** loop over the links **/
		foreach ($links as $link)
		{					
			/** Get file extension **/
			$file_ext = end(explode('.',$link->nodeValue));
						
			/** Default image for unknown file types **/
			$file_image = "unknown.png";
			
			switch ($file_ext) {
				case 'doc':
					$file_image = "doc-rtf.png";
					break;
				case 'rtf':
					$file_image = "doc-rtf.png";
					break;				
				case 'fla':
					$file_image = "fla.png";
					break;
				case 'html':
					$file_image = "html-htm-ff.png";
					break;
				case 'htm':
					$file_image = "html-htm-ff.png";
					break;
				case 'mov':
					$file_image = "mov.png";
					break;
				case 'pdf':
					$file_image = "pdf.png";
					break;
				case 'ppt':
					$file_image = "ppt.png";
					break;
				case 'psd':
					$file_image = "psd.png";
					break;
				case 'swf':
					$file_image = "swf.png";
					break;
				case 'xls':
					$file_image = "xls.png";
					break;
				case 'zip':
					$file_image = "zip-rar.png";
					break;
				case '7zip':
					$file_image = "zip-rar.png";
					break;
				case 'rar':
					$file_image = "zip-rar.png";
					break;
			}
			
			/** image location **/
			$file_image = $image_dir.$file_image;
						
			/** file location **/
			$file_link = $link->getAttribute('href');			
			
			echo "<a href=\"$file_link\"><img src=\"$file_image\" /></a>";
			
		} 	
	
	} 
iva2k’s picture

Status: Fixed » Needs work

@grand_master_v - Good to hear you solved your problem.

Looking at the issue and proposed code, I think the issue deserves some solution functioning "out of the box" for users of the module. This will probably include the use of module's .CSS and sprite images for mime types. Unfortunately, solution in #2 uses a set of separate images, and does not rely on CSS, which makes it hard and questionable to include into the module's code. Also, that approach makes no use of thumbnails.

Given that "fixed" status means that solution is committed into code & CVS, which it is not, I'm changing to "needs work" to indicate that there's a need to do some more R&D. That probably needs to be proper integration with views (via hooks), using existing sprites and thumbnails and relying on CSS which maps MIME types to sprite position. I have no bandwidth to look into that. Anyone up to the task? I will review all submitted patches.

vincetingey’s picture

Hi Ilya,

Thanks for looking into this. I know my solution is just a kludgy work around but it did solve my problem. I agree that proper out of the box integration would be best, however I do not have the expertise or time to code this either. Also sorry for setting the ticket to fixed. Since I found a solution to my problem I thought that I should change the status to fixed.

Cheers!
- Vince

geerlingguy’s picture

StatusFileSize
new52.57 KB

If you could add a new field either under the 'Upload' set of fields or under another heading, that would be best. The field could be called 'Attached file icon' or something like that (see screenshot of views edit page).

(This would have to work around the problem of multiple files per node, of course... which could be tricky).

geerlingguy’s picture

StatusFileSize
new2.78 KB

I've attached an alternate template I'm using with my view to simply grab a link and print an image (using the images provided by iTweak instead of my own images). (Note: I've also updated my script on my site to use PHP's pathinfo() function to grab the file extension, as it's more secure/faster).

shenzhuxi’s picture

Better solution for #6
Use file mime instead

if($output){
/** Define image root directory **/
$image_dir = drupal_get_path('module', 'itweak_upload') . '/images/mime/16/';
/** Default image for unknown file types **/
$file_image = "_default.png";

switch ($output) {
case 'application/postscript':
$file_image = "ai.png";
break;
case 'application/msword':
$file_image = "doc.png";
break;
case 'application/x-gtar':
$file_image = "tar.png";
break;
case 'id':
$file_image = "id.png";
break;
case 'image/jpeg':
$file_image = "jpeg.png";
break;
case 'image/jpeg':
$file_image = "jpeg.png";
break;
case 'application/pdf':
$file_image = "pdf.png";
break;
case 'image/png':
$file_image = "png.png";
break;
case 'application/vnd.ms-powerpoint':
$file_image = "pptx.png";
break;
case 'psd':
$file_image = "psd.png";
break;
case 'mov':
$file_image = "quicktime.png";
break;
case 'mp4':
$file_image = "quicktime.png";
break;
case 'm4v':
$file_image = "quicktime.png";
break;
case 'application/rar':
$file_image = "rar.png";
break;
case 'image/svg+xml':
$file_image = "svg.png";
break;
case 'application/x-shockwave-flash':
$file_image = "swf.png";
break;
case 'application/vnd.ms-excel':
$file_image = "xlsx.png";
break;
case 'application/zip':
$file_image = "zip.png";
break;
}

/** image location **/
$file_image = $image_dir.$file_image;
print theme('image', $file_image);
}

iva2k’s picture

Version: 6.x-2.3 » 6.x-2.x-dev
Category: support » feature

This would be a nice feature.

I can't tidy it up due to lack of free time, but will review a proper patch.

Standards to aim for:
- should use CSS and mime image sprites (same as the method already used by iTweak Upload)
- should be views API code, not a template file to put into theme directory (to have no additional user installation steps)
- patch should conform to Drupal guidelines, and be based on 6.x-2.x-dev code

lolandese’s picture

Subscribe

iva2k’s picture

Status: Needs work » Closed (won't fix)

I'm stopping any development for D6 version of this module. Still open to review submitted patches. There is a hope that D7 code may get some attention (D7 has MIME icons support in core so it may be already solved there), but due to complete rewrite it is not going to be portable.

iva2k’s picture

Title: Display icons in views » Display icons and thumbnails in views

I assume thumbnails are also desirable to have.