2

I'm attempting to add a custom icon for file uploads via a module. However, all I'm finding online is advice on using a theme function (How to change File field icons (without changing core)?). I want this in a module to make it work across various themes. I found file_icon_path, but am not sure how to implement it. How would this be done in a module?

2

I think you would want to do something along these lines:

function YOURMODULE_theme_registry_alter(&$theme_registry) {

  if (!empty($theme_registry['file_icon']['function'])) {
    $theme_registry['file_icon']['function'] = 'YOURMODULE_file_icon';
  }

}

and then create YOURMODULE_file_icon() based upon the core theme_file_icon():

/**
 * Returns HTML for an image with an appropriate icon for the given file.
 *
 * @param $variables
 *   An associative array containing:
 *   - file: A file object for which to make an icon.
 *   - icon_directory: (optional) A path to a directory of icons to be used for
 *     files. Defaults to the value of the "file_icon_directory" variable.
 *
 * @ingroup themeable
 */
function YOURMODULE_file_icon($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];

  $mime = check_plain($file->filemime);
  $icon_url = file_icon_url($file, $icon_directory);

  return '<img class="file-icon" alt="" title="' . $mime . '" src="' . $icon_url . '" />';
}

and change up either $icon_directory to point within your module and include all of the various file type icons there, or do a switch or if on the $mime type and grab them from wherever you want, eg, if it's the special mime type you want, grab them from your module's directory, otherwise, grab them from the core/default directory.

6
  • Being dense here. If theme_file_icon is a normal theme function, then why the manual registry alter? Shouldn't a theme registry rescan pick up the override? – mpdonadio Jan 8 '14 at 2:32
  • I'm also curios why core builds up the img manually, rather than via theme('image') – mpdonadio Jan 8 '14 at 2:35
  • I was curious about your latter comment as well @MPD, but that's what it does, I just copied it verbatim. As for your first, it's my understanding that the scans just pick up theme overrides, eg, THEMENAME_xyzzy(), not MODULENAME_xyzzy(), but I could very well be mistaken and making this far too difficult. – Jimajamma Jan 8 '14 at 3:33
  • @Jimajamma, thanks for your help, that seems to be working to register the function, which I can apparently name whatever, but I'll keep that naming convention. Now, I have changed the $icon_directory to my module's icon directory (mymodule/images/icons), but it looks like the file_icon_url() function doesn't like it for some reason and returns no path. What could I be doing wrong now? – Jav Jan 8 '14 at 17:30
  • 1
    not sure, I'd take a look at the file_icon_path() function in the file.module and make sure your naming conventions match what it is expecting, both with respect to the path as well as the image filenames. Most notably make sure your path is prefaced with something along the lines of drupal_get_path('module', 'YOURMODULE') and that the icons are in the expected name.ext format. – Jimajamma Jan 8 '14 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.