2016-07-28 7 views
2

Ich versuche, den Einstellungen meines Themas ein "Datei" -Typfeld hinzuzufügen. Ich verwende kein Basisthema und arbeite in Drupal 7. Das Feld erscheint an der richtigen Stelle und ich kann eine Datei auswählen, aber wenn ich die Einstellungen speichere, wird die Datei nicht in meinem Dateiordner angezeigt und ausgeführt theme_get_settings für die Einstellung gibt eine leere Zeichenfolge zurück. Was mache ich falsch?Hinzufügen eines Dateityp-Formularfelds zu Drupal-Einstellungen für das Systemthema

Hier ist mein Feldcode:

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

Antwort

0

Wenn Sie eine Datei Formular-Steuerelement zu verwenden, müssen Sie eine validate hinzufügen und einreichen Methode (oder die bestehende verändern), um die Datei selbst hochladen.

Sie können an der Standard-System Thema Methode einen Blick, Hexe können Sie das Favicon und die Logo-Dateien mit Ihrem Thema zu verwenden, um definieren: => In der Datei /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']); 
    } 
... 
} 

bearbeiten

Wie ich unten im Kommentar sagte, müssen Sie die Datei in der ‚footer_logo‘ Combo empfangen speichern, und füllen Sie den ‚footer_logo_path‘ Wert mit dem Pfad der gespeicherten Datei.

Dann wird es kein Problem sein, wenn die Methoden zweimal ausgeführt werden.

+0

Danke für die Antwort, Tytoo! Ich habe neue benutzerdefinierte Validierungs- und Übergabefunktionen hinzugefügt und lasse sie laufen, aber sie scheinen zweimal ausgeführt zu werden. Wie kann ich das verhindern? – Colin

+0

Nur zur Verdeutlichung, meine benutzerdefinierte Übermittlungs-/Validierungsfunktion wird zuerst ausgeführt, gefolgt von der Standardeinstellung. – Colin

+0

@Colin, ich weiß nicht, warum Ihre Formularvalidierungs- und Übermittlungsmethoden zweimal ausgeführt werden. Wenn Sie jedoch wie im Beispiel vorgehen, muss Ihre Datei 'footer_logo' transformiert werden und der Pfad der hochgeladenen Datei muss in den Wert 'footer_logo_path' eingegeben werden. – TytooF

Verwandte Themen