为外观设置文件上载设置FILE_STATUS_PERMANENT

时间:2012-01-09 18:47:41

标签: php drupal drupal-7

在Drupal 7中,我在theme-settings.php中有一个主题包含以下代码:

function greytheme_form_system_theme_settings_alter(&$form, $form_state)
{
    $form["greytheme_home_img"] = array(
        '#type'          => 'managed_file',
        '#title'         => t('Home Page Image'),
        '#description'   => t("Home page image to be used in the ribbon."),
        // fileID (fid) used to display the file later
        '#default_value' => variable_get('greytheme_home_img', ''), 
        '#upload_location' => 'public://homeimg/',
        '#upload_validators' => array(
            'file_validate_extensions' => array("png jpg jpeg gif"),
        ),
    );
}

这将允许我在此主题的外观设置管理屏幕上上传和验证图像。

文件按预期写入文件系统。但是,文件上载未在数据库中标记为FILE_STATUS_PERMANENT,导致它稍后被cronjob扫除。如何“捕获”此文件上传的结果,以便我可以设置FILE_STATUS_PERMANENT并获取fid对象的managed_file?我觉得它与system_theme_settings_submit()有关,但我不确定要覆盖什么。

2 个答案:

答案 0 :(得分:3)

您只需要向表单添加自定义提交处理程序,您可以从那里抓取fid

function greytheme_form_system_theme_settings_alter(&$form, $form_state) {
  $form["greytheme_home_img"] = array(
    '#type'          => 'managed_file',
    '#title'         => t('Home Page Image'),
    '#description'   => t("Home page image to be used in the ribbon."),
    // fileID (fid) used to display the file later
    '#default_value' => variable_get('greytheme_home_img', ''), 
    '#upload_location' => 'public://homeimg/',
    '#upload_validators' => array(
        'file_validate_extensions' => array("png jpg jpeg gif"),
    ),
  );

  $form['#submit'][] = 'greytheme_system_theme_settings_form_submit';
}

function greytheme_system_theme_settings_form_submit(&$form, &$form_state) {
  $fid = $form_state['values']['greytheme_home_img'];
  $file = file_load($fid);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
}

答案 1 :(得分:0)

为什么使用variable_get('greytheme_home_img', '')?这对我不起作用,所以我使用了theme_get_setting('greytheme_home_img')