【问题标题】:Adding File Type Form Field to Drupal System Theme Settings将文件类型表单字段添加到 Drupal 系统主题设置
【发布时间】:2016-07-28 14:50:38
【问题描述】:

我正在尝试将“文件”类型字段添加到我的主题设置中。我没有使用基本主题,而是在 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() . '://',
);

【问题讨论】:

    标签: php drupal drupal-7 drupal-forms


    【解决方案1】:

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

    您可以查看默认的系统主题方法,它允许您定义网站图标和徽标文件以与您的主题一起使用: => 在文件 /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;
      }
    

    如果方法运行两次就不会麻烦了。

    【讨论】:

    • 感谢您的回复,Tytoo!我添加了新的自定义验证和提交函数并让它们运行,但它们似乎运行了两次。我怎样才能防止这种情况发生?
    • 澄清一下,我的自定义提交/验证函数首先运行,然后是默认值。
    • @Colin,我不知道为什么您的表单验证和提交方法会运行两次。但是如果你按照例子那样做,你的'footer_logo'文件必须被转换并且上传的文件路径必须放在'footer_logo_path'值中。
    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多