Управление файлами в Drupal7

/**
* our example system settings form
*/
function mf_settings_form($form,&$form_state) {
$form = array();

$form['managed_file_example'] = array(
'#type' => 'managed_file',
'#title' => t('Managed File Example'),
'#default_value' => variable_get('managed_file_example',''),
'#upload_location' => 'public://managed_file_example/',
);

$form = system_settings_form($form);
$form['#submit'][] = 'mf_settings_submit';

return $form;
}


/**
* our processing form for managed fields. because system settings forms unset the
* system variables before we get here, we need to check the default value from
* the form and see if it matches what we are getting from the submitted values.
*/

function mf_settings_submit($form, $form_state) {
/* get the original default files if one extsis */
$managed_file_example = (!empty($form['managed_file_example']['#default_value'])) ? $form['managed_file_example']['#default_value'] : 0;
/* if we have been provided files, make sure to mark them as in use */
if (isset($form_state['values']['managed_file_example']) && $form_state['values']['managed_file_example']) {
/**
* if the uploaded file is different than the one we already have, then we need to
* remove the current file and replace it with the new one
*/
if (isset($managed_file_example) && $managed_file_example != $form_state['values']['managed_file_example']) {
mf_remove_managed_file($managed_file_example, 'one');
mf_add_managed_file($form_state['values']['managed_file_example'], 'example');
}
else {
mf_add_managed_file($form_state['values']['managed_file_example'], 'example');
}
}
elseif ($managed_file_one) {
mf_remove_managed_file($managed_file_example, 'example');
}
}

function mf_remove_managed_file($managed_file, $which) {
// Retrieve the old file's id.
$file = $managed_file ? file_load($managed_file) : FALSE;
if ($file) {
// When a module is managing a file, it must manage the usage count.
// Here we decrement the usage count with file_usage_delete().
file_usage_delete($file, 'mf', $which, $file->fid);

// The file_delete() function takes a file object and checks to see if
// the file is being used by any other modules. If it is the delete
// operation is cancelled, otherwise the file is deleted.
file_delete($file);
drupal_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename)));
}
}

function mf_add_managed_file($managed_file, $which) {
/* if our file is already in use, then we don't need to re-do this and increase the count */
$count = db_query('SELECT `count` FROM {file_usage} WHERE fid=:fid', array('fid' => $managed_file))->fetchField();
if (empty($count)) {
/* load the file via fid */
$file = file_load($managed_file);
/* change status to permanent */
$file->status = FILE_STATUS_PERMANENT;
/* save the file */
file_save($file);
/* record the file as in use */
file_usage_add($file, 'mf', $which, $managed_file);
unset($file);
}
}

Категория: 
The code has been tested and works

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.