Posted by phponwebsites on February 24, 2015 at 9:20am
Normally we can upload images programmatically.
I enabled latest version of media module to upload images in wysiwyg. It is working fine. There is no problem. But i want to upload images programmatically from media library. That means, while clicking browse field, it opened pop up box to select/upload images from media library.
Below is my code.
function test_form_upload_form_alter(&$form, $form_state) {
$form['settings']['apply_now_image'] = array(
'#type' => 'media',
'#title' => 'Image for apply now',
);
$form['#submit'][] = 'test_upload_form_submit';
}
function test_upload_form_submit(&$form, $form_state) {
if (!empty($form_state['values']['apply_now_image'])) {
$file = file_load($form_state['values']['apply_now_image']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
file_usage_add($file, 'user', 'user', $user->uid);
$apply_now_image_path = file_create_url($file->uri);
variable_set('rgit_apply_now_image', $apply_now_image_path);
variable_set('rgit_apply_now_image1', $file->fid);
}
}
But it didn't give correct answer. This is only work if the '#type' is managed_file'. It didn't work if the '#type' is media. Is any solutions there to upload images from media library.