Skip to content

Instantly share code, notes, and snippets.

@mkorostoff
Last active August 29, 2015 14:07
Show Gist options
  • Save mkorostoff/b7a059cd3221e00bb4a9 to your computer and use it in GitHub Desktop.
Save mkorostoff/b7a059cd3221e00bb4a9 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_theme_registry_alter()
*/
function radius_settings_theme_registry_alter(&$theme_registry) {
$theme_registry['file_link']['function'] = 'radius_settings_file_link';
}
/**
* Change the file upload indicator from a text link, to a thumbnail. This
* overrides theme_file_link
*
* Default behavior: http://i.imgur.com/R6VEqMY.gif
* Desired behavior: http://i.imgur.com/pBt54DR.png
*
* This is used exclusively on settings/personal-info
*
* @see
* _radius_settings_personal_form_submit
*/
function radius_settings_file_link($vars) {
if (
(current_path() === 'settings/personal-info') ||
//This first condition will evaluate as true during an AJAX image upload on settings/personal-info
(!empty($_REQUEST['form_id']) && $_REQUEST['form_id'] === 'radius_settings_personal_form')
) {
global $user;
$image = $vars['file'];
$extension = strtolower(substr($vars['file']->filemime, 6));
$extension = ($extension === 'jpeg') ? 'jpg' : $extension;
/**
* During AJAX upload we want to make a temporary copy of this file in the
* public:// directory, so that the user can preview their upload. This
* avoids the behavior shown in this gif http://i.imgur.com/skH1Zl5.gif
*/
if (preg_match('/^temporary/', $image->uri)) {
$image = file_copy($image, 'public://temp/' . $user->name . '.' . $extension);
/**
* Store this image in the session, and then delete it when the user
* submits the form.
*
* @todo
* handle form abandonment, by occasionally deleting the temporary store
* of images on cron
*/
$_SESSION['images_to_delete'][] = $image;
}
$content = array(
'file' => array(
'#theme' => 'image_style',
'#style_name' => '200x200_square',
'#path' => $image->uri,
'#width' => (isset($image->info['width'])) ? $image->info['width'] : NULL,
'#height' => (isset($image->info['height'])) ? $image->info['height'] : NULL,
),
);
return drupal_render($content);
}
else {
return theme_file_link($vars);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment