phpdrupaldrupal-7drupal-forms

Adding File Type Form Field to Drupal System Theme Settings


I'm trying to add a "file" type field to my theme's settings. I'm not using a base theme and am working in Drupal 7. The field shows up in the correct place and I can select a file, but, when I save the settings, the file does not show up in my files folder and running theme_get_settings on the setting returns an empty string. What am I doing incorrectly?

Here is my field code:

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

Solution

  • When you use a file form control, you have to add a validate and a submit method (or modify the existing ones) to upload the file itself.

    You can have a look at the default system theme method, witch allow you to define the favicon and the logo files to use with your theme : => In the file /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']);
      }
    ...
    }
    

    Edit

    As I said in the comment below, you must save the file received in the 'footer_logo' combo, and populate the 'footer_logo_path' value with the path of the saved file.

      // 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;
      }
    

    Then it will not be a trouble if the methods are running twice.