Skip to content

Instantly share code, notes, and snippets.

@micahredding
Created March 19, 2013 15:38
Show Gist options
  • Save micahredding/5197132 to your computer and use it in GitHub Desktop.
Save micahredding/5197132 to your computer and use it in GitHub Desktop.
How to upload a file in a block configure form
function hook_block_configure($delta = '') {
$form['image'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
'#upload_location' => 'public://header_blocks/',
);
}
function hook_block_save($delta, $edit) {
foreach($edit as $key => $updated_content ) {
if($key == 'image') {
if(isset($edit[$key]) && is_numeric($edit[$key]) && $edit[$key] > 0) {
$file = file_load($edit[$key]);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'header_blocks', 'node', $nid);
}
}
}
}
@micahredding
Copy link
Author

$key = 'image';
if(isset($edit[$key]) && is_numeric($edit[$key]) && $edit[$key] > 0) {
$file = file_load($edit[$key]);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'header_blocks', 'node', $nid);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment