0

I am trying to create functionality where administrator can upload header image whenever he wants.

I' using hook_form_system_theme_settings_alter in theme-settings.php and altering form like this:

 $form['theme_settings']['background_file'] = array(
        '#type' => 'managed_file',
        '#title' => t('Background'),
        '#required' => TRUE,
        '#upload_location' => file_default_scheme() . '://public/backgrounds/',
        '#default_value' => theme_get_setting('background_file'),
        '#upload_validators' => array(
            'file_validate_extensions' => array('gif png jpg jpeg'),
        ),
    );

in template.php using hook_preprocess_page and doing this in it:

$fid = theme_get_setting('background_file');
$loaded = file_load($fid);
$variables['background_url'] = file_create_url($loaded->uri);

and trying to reach $variables['background_url'] from tpl.php file.

When I upload image everything works great, header image is set properly and code has no problem but after a while, I don't really know what happens, for example after 1 day file_load returns FALSE, I just reupload image and everything works fine again.

theme_get_setting('background_file'); always returns file id without problems.

I am missing something? I can't find source of problem.

Thanks.

1

I think it's because there is a problem in your template.php. Indeed drupal should save your file only temporarly. You have to save your file permenantly with this code

$file = file_load($form_state['values']['background_file']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.