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

时间:2016-07-28 14:50:38

标签: php drupal drupal-7 drupal-forms

我试图添加"文件"在我的主题设置中键入字段。我没有使用基本主题并在Drupal 7中工作。该字段显示在正确的位置,我可以选择一个文件,但是,当我保存设置时,该文件不会显示在我的文件夹中并在该设置上运行theme_get_settings返回一个空字符串。我做错了什么?

这是我的字段代码:

// 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() . '://',
);

1 个答案:

答案 0 :(得分:0)

使用文件表单控件时,必须添加validate和submit方法(或修改现有方法)以自行上传文件。

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

#Gemfile
gem 'sqlite3', group: [:development, :test]
gem 'pg', group: :production

修改

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

...
    $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']);
  }
...
}

如果方法运行两次,那将不会有麻烦。