在自定义模块中上传图片


8

我正在编写一个自定义模块,并且需要它来上传图像。我很难找到关于此的好的文档,但是我想我已经接近了。

我想念什么?$ file在表单提交中返回false。

function mymodule_custom_content_block_form($form_state){
    $form = array();
    $form['custom_content_block_text'] = array(
        '#type' => 'textarea',
        '#title' => t('Block text'),
        '#default_value' => variable_get('mymodule_custom_content_block_text'),
        '#required' => true,
    );
    $form['custom_content_block_image'] = array(
        '#type' => 'file',
        '#name' => 'custom_content_block_image',
        '#title' => t('Block image'),
        '#size' => 40,
        '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
    );  
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
    );
    return $form;
}

function mymodule_custom_content_block_form_submit($form, &$form_state){
    if(isset($form_state['values']['custom_content_block_image'])){
        $validators = array('file_validate_extensions' => array('jpg jpeg'));
        $file = file_save_upload('custom_content_block_image', $validators, 'public://');
        if($file == false){
            drupal_set_message(t("Error saving image."), $type = "error", $repeat = false);
        }
        else{
            $file->status = FILE_STATUS_PERMANENT;
            $file = file_save($file);   
        }
    }
    variable_set('mymodule_custom_content_block_text', $form_state['values']['custom_content_block_text']);
    drupal_set_message(t('Custom Content Block has been updated.'));
}

Answers:


19

如果您不介意我的话,那么您就很难做到这一点。Drupal的managed_file元素类型可以为您处理大多数逻辑:

function mymodule_custom_content_block_form($form, &$form_state) {
  $form['custom_content_block_image'] = array(
    '#type' => 'managed_file',
    '#name' => 'custom_content_block_image',
    '#title' => t('Block image'),
    '#size' => 40,
    '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
    '#upload_location' => 'public://'
  ); 

  return $form; 
}

function mymodule_custom_content_block_form_submit($form, &$form_state) {
  if (isset($form_state['values']['custom_content_block_image'])) {
    $file = file_load($form_state['values']['custom_content_block_image']);

    $file->status = FILE_STATUS_PERMANENT;

    file_save($file);
  }
}

应当指出的是,file_save只能Drupal的6之后
威尔

4

使用Clive回答,我的图片在6小时后被删除。因此,如果有人遇到我遇到的相同问题。这是解决方案(来自Clive的回答,还有一点点补充)。

function mymodule_custom_content_block_form($form, &$form_state) {
  $form['custom_content_block_image'] = array(
    '#type' => 'managed_file',
    '#name' => 'custom_content_block_image',
    '#title' => t('Block image'),
    '#size' => 40,
    '#description' => t("Image should be less than 400 pixels wide and in JPG format."),
    '#upload_location' => 'public://'
  ); 

  return $form; 
}

function mymodule_custom_content_block_form_submit($form, &$form_state) {
  if (isset($form_state['values']['custom_content_block_image'])) {
    $file = file_load($form_state['values']['custom_content_block_image']);

    $file->status = FILE_STATUS_PERMANENT;

    $file_saved =file_save($file);
    // Record that the module is using the file. 
    file_usage_add($file_saved, 'mymodule_custom_content_block_form', 'custom_content_block_image', $file_saved->fid); 
  }
}

解决方法是添加file_usage_add。从API文档中:

注意:新文件的状态为0上载,并被视为临时文件,这些文件将在6个小时后通过cron删除。您的模块负责将$ file对象的状态更改为FILE_STATUS_PERMANENT并将新状态保存到数据库。提交处理程序中的诸如此类的东西应该可以解决问题。

请参阅:https : //api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#managed_file


1

应该将此属性添加到您的表单中,以便它可以处理文件上传。

$form['#attributes']['enctype'] = "multipart/form-data";
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.