Last active
September 22, 2016 14:43
-
-
Save laurelstreng/7efc63a74d84b6d4e04ce00f158cd5f7 to your computer and use it in GitHub Desktop.
Drupal 7 - Theme Settings Image Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (theme_get_setting('new_logo')): ?> | |
<div class="new-logo"> | |
<?php | |
$fid = theme_get_setting('new_logo'); | |
$image_url = file_create_url(file_load($fid)->uri); | |
?> | |
<img src="<?php echo $image_url; ?>"/> | |
</div> | |
<?php endif; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function THEMENAME_form_system_theme_settings_alter(&$form, $form_state, $form_id = NULL){ | |
$default_file_dir = 'public://'; | |
$folder = file_prepare_directory($default_file_dir, FILE_CREATE_DIRECTORY); | |
$settings_theme = $form_state['build_info']['args'][0]; | |
if ($folder) { | |
$new_logo = theme_get_setting('new_logo'); | |
// BUG: Force file to be permanent. | |
if (!empty($new_logo)) { | |
_fix_permanent_image('new_logo', $settings_theme); | |
} | |
$form['new_logo'] = array( | |
'#type' => 'managed_file', | |
'#title' => t('New Logo'), | |
'#description' => t("Image should be less than 400 pixels wide and in PNG format."), | |
'#default_value' => $new_logo, | |
'#progress_message' => t('Please wait...'), | |
'#progress_indicator' => 'bar', | |
'#upload_location' => $default_file_dir, | |
'#upload_validators' => array( | |
'file_validate_extensions' => array('png') | |
), | |
); | |
} | |
} | |
function _fix_permanent_image($image, $theme) { | |
$fid = theme_get_setting($image, $theme); | |
if ($fid > 0) { | |
$file = file_load($fid); | |
if (is_object($file) && $file->status == 0) { | |
$file->status = FILE_STATUS_PERMANENT; | |
file_save($file); | |
file_usage_add($file, $theme, 'theme', 1); | |
drupal_set_message($image . ' saved permanently.', 'status'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment