Hello,
I'm new to Drupal and need help. I'm trying to create a way to upload a background image to my theme called shows. The code I'm using is below:

<?php
function shows_form_system_theme_settings_alter(&$form, $form_state) {
$form['background_file'] = array(
'#type'     => 'managed_file',
'#title'    => t('Background'),
'#required' => True,
'#upload_location' => file_default_scheme() . '://theme/backgrounds/',
'#default_value' => theme_get_setting('background_file'), 
'#upload_validators' => array('file_validate_extensions' => array('gif png jpg jpeg'),
),);}
?>

When I upload the image, I get this error message:

Notice: Undefined index: system_theme_settings in drupal_retrieve_form() (line 806 >of /Applications/MAMP/htdocs/newtower.dev/includes/form.inc).

Warning: call_user_func_array() expects parameter 1 to be a valid callback, >function 'system_theme_settings' not found or invalid function name in >drupal_retrieve_form() (line 841 of /Applications/MAMP/htdocs/newtower.dev/includes/form.inc).

Notice: Undefined index: background_file in file_ajax_upload() (line 271 of /Applications/MAMP/htdocs/newtower.dev/modules/file/file.module).

Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of /Applications/MAMP/htdocs/newtower.dev/modules/file/file.module).

Any ideas on how to fix the problem?

Thanks,
Gregory S.

Comments

Kuldip Gohil’s picture

Hi Greogory,

This code works perfectly fine with me.

I've tried by putting this in my admin theme ("seven")'s template.php file.

Hope you'd put this code in your template.php file of "shows" theme.

Thanks,
Kuldip Gohil

openbayou’s picture

Hi again,

I'm using theme-settings.php, renamed the file to template.php and the upload went away. Renaming it back to theme-settings returned the upload.

I left out an important part that I'm using domain access for multisites. If I use it globally across my network, it works. When I go to each domain and upload a background image, I get the error message.

Gregory S.

Dev1.addweb’s picture

This code should be ideally in "theme-settings.php" file.

The form needs to be able to find system_theme_settings() when getting rebuilt via ajax.

Writing form_load_include($form_state, 'inc', 'system', 'system.admin'); inside of theme-settings.php hook_form_system_theme_settings_alter() should do the trick.

Let me know if you face any query/concern regarding this.

Thanks

openbayou’s picture

Added update, still get error messages. Important part I left out is this is a multisite installation with domain access.

<?php
function shows_form_system_theme_settings_alter(&$form, $form_state) {
$form['background_file'] = array(
'#type'     => 'managed_file',
'#title'    => t('Background'),
'#upload_location' => file_default_scheme() . '://theme/backgrounds/',
'#default_value' => theme_get_setting('background_file'), 
'#upload_validators' => array(
  'file_validate_extensions' => array('gif png jpg jpeg'),
),);
form_load_include($form_state, 'inc', 'system', 'system.admin');
}
?>

UPDATE: New code. Error message don't show up but now it won't save the file.

<?php
function shows_form_system_theme_settings_alter(&$form, $form_state) {
$form['background_file'] = array(
'#type'     => 'managed_file',
'#title'    => t('Background'),
'#upload_location' => file_default_scheme() . '://theme/backgrounds/',
'#default_value' => theme_get_setting('background_file'), 
'#upload_validators' => array(
  'file_validate_extensions' => array('gif png jpg jpeg'),
),);
}
form_load_include($form_state, 'inc', 'system', 'system.admin');
?>
openbayou’s picture

Hey,

I rearranged the code and now it works. It uploads the image without error messages.

<?php
function tsaudio_form_system_theme_settings_alter(&$form, $form_state) {
  $form['background_file'] = array(
'#type'     => 'managed_file',
'#title'    => t('Background'),
'#upload_location' => file_default_scheme() . '://theme/backgrounds/',
'#default_value' => theme_get_setting('background_file'), 
'#upload_validators' => array(
  'file_validate_extensions' => array('gif png jpg jpeg'),
),);
form_load_include($form_state, 'inc', 'system', 'system.admin');
}
?>

So how do I display the URL of the image? The form is called 'background_file'. I don't need to display the image, I just need the URL to the image.

openbayou’s picture

I get this error message when I delete the file. What does it mean and how to fix it?

    Notice: Undefined index: system_theme_settings in drupal_retrieve_form() (line 806 of /Users/gregoryschultz/Sites/towersignal.dev/includes/form.inc).
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'system_theme_settings' not found or invalid function name in drupal_retrieve_form() (line 841 of /Users/gregoryschultz/Sites/towersignal.dev/includes/form.inc).
    Notice: Undefined index: background_file in file_ajax_upload() (line 271 of /Users/gregoryschultz/Sites/towersignal.dev/modules/file/file.module).
    Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of /Users/gregoryschultz/Sites/towersignal.dev/modules/file/file.module).
Dev1.addweb’s picture

To display the URL of the image use following code file_create_url(file_load(theme_get_setting('background_file'))->uri)

Also i suggest clear your cache as the solution above worked for me.

Also you can try changing the line to one of this:

form_load_include($form_state, 'inc', 'user', 'user.pages');
form_load_include($form_state, 'inc', 'node', 'node.pages');

Hope this helps you.

Thanks!