将文件类型表单字段添加到 Drupal 系统主题设置

Adding File Type Form Field to Drupal System Theme Settings

我正在尝试将 "file" 类型字段添加到我的主题设置中。我没有使用基本主题,而是在 Drupal 7 中工作。该字段显示在正确的位置,我可以 select 一个文件,但是,当我保存设置时,该文件没有显示在我的文件夹和 运行 theme_get_settings 设置 returns 一个空字符串。我做错了什么?

这是我的字段代码:

// footer settings
$form['footer_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Footer Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
);
$form['footer_settings']['footer_logo_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Footer Logo Path'),
    '#default_value' => theme_get_setting('footer_logo', ''),
);
$form['footer_settings']['footer_logo'] = array(
    '#type' => 'file',
    '#title' => t('Footer Logo'),
    '#description' => t('Upload a new logo image to be displayed in the footer of the website here.'),
    '#upload_location' => file_default_scheme() . '://',
);

当你使用文件表单控件时,你必须添加一个验证和一个提交方法(或修改现有的)来上传文件本身。

您可以查看默认的系统主题方法, 允许您定义 favicon 和徽标文件以用于您的主题: => 在文件中 /modules/system/system.admin.inc

...
    $form['logo']['settings']['logo_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload logo image'),
      '#maxlength' => 40,
      '#description' => t("If you don't have direct file access to the server, use this field to upload your logo.")
    );
...
  $form['#submit'][] = 'system_theme_settings_submit';
  $form['#validate'][] = 'system_theme_settings_validate';
...
/**
 * Validator for the system_theme_settings() form.
 */
function system_theme_settings_validate($form, &$form_state) {
  // Handle file uploads.
  $validators = array('file_validate_is_image' => array());

  // Check for a new uploaded logo.
  $file = file_save_upload('logo_upload', $validators);
  if (isset($file)) {
    // File upload was attempted.
    if ($file) {
      // Put the temporary file in form_values so we can save it on submit.
      $form_state['values']['logo_upload'] = $file;
    }
    else {
      // File upload failed.
      form_set_error('logo_upload', t('The logo could not be uploaded.'));
    }
  }

  $validators = array('file_validate_extensions' => array('ico png gif jpg jpeg apng svg'));
...
  // If the user provided a path for a logo or favicon file, make sure a file
  // exists at that path.
  if (!empty($form_state['values']['logo_path'])) {
    $path = _system_theme_settings_validate_path($form_state['values']['logo_path']);
    if (!$path) {
      form_set_error('logo_path', t('The custom logo path is invalid.'));
    }
  }
  if (!empty($form_state['values']['favicon_path'])) {
    $path = _system_theme_settings_validate_path($form_state['values']['favicon_path']);
    if (!$path) {
      form_set_error('favicon_path', t('The custom favicon path is invalid.'));
    }
  }
}
...

/**
 * Process system_theme_settings form submissions.
 */
function system_theme_settings_submit($form, &$form_state) {
...
  if (!empty($values['logo_upload'])) {
    $file = $values['logo_upload'];
    unset($values['logo_upload']);
    $filename = file_unmanaged_copy($file->uri);
    $values['default_logo'] = 0;
    $values['logo_path'] = $filename;
    $values['toggle_logo'] = 1;
  }

  // If the user entered a path relative to the system files directory for
  // a logo or favicon, store a public:// URI so the theme system can handle it.
  if (!empty($values['logo_path'])) {
    $values['logo_path'] = _system_theme_settings_validate_path($values['logo_path']);
  }
...
}

编辑

正如我在下面的评论中所说,您必须保存在 'footer_logo' 组合中收到的文件,并使用保存文件的路径填充 'footer_logo_path' 值。

  // If the user uploaded a new logo, save it to a permanent location
  if (!empty($values['footer_logo'])) {
    $file = $values['footer_logo'];
    unset($values['footer_logo']);
    $filename = file_unmanaged_copy($file->uri);
    $values['footer_logo_path'] = $filename;
  }

那么方法是运行两次就不麻烦了