2016-07-28 55 views
2

我想添加一個「文件」類型字段到我的主題的設置。我沒有使用基本主題,並且在Drupal 7中工作。該字段顯示在正確的位置,我可以選擇一個文件,但是當我保存設置時,文件不會顯示在我的文件夾中並且正在運行該設置上的theme_get_settings返回一個空字符串。我做錯了什麼?添加文件類型窗體字段到Drupal的系統主題設置

這裏是我的域代碼:

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

回答

0

當您使用文件表單控件,您必須添加一個驗證及提交方法(或修改現有的)上傳的文件本身。

你可以看一下默認的系統主題法,巫婆允許你定義的圖標和標識的文件與你要使用的主題: =>在文件/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; 
    } 

然後,如果方法運行兩次,這將不會是一個麻煩。

+0

感謝您的迴應,Tytoo!我添加了新的自定義驗證和提交功能並讓它們運行,但它們似乎運行了兩次。我怎樣才能防止這種情況發生? – Colin

+0

爲了澄清,我的自定義提交/驗證功能首先運行,然後是默認。 – Colin

+0

@Colin,我不知道爲什麼你的表單驗證和提交方法運行兩次。 但是,如果您按照示例進行操作,則必須對您的'footer_logo'文件進行轉換,並且必須將上傳的文件路徑放入'footer_logo_path'值中。 – TytooF

相關問題