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
.
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
Update
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.
RE: Can't save custom code on theme settings
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.phphook_form_system_theme_settings_alter()
should do the trick.Let me know if you face any query/concern regarding this.
Thanks
update with new code
Added update, still get error messages. Important part I left out is this is a multisite installation with domain access.UPDATE: New code. Error message don't show up but now it won't save the file.
success!
Hey,
I rearranged the code and now it works. It uploads the image without error messages.
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.
error when deleting file
I get this error message when I delete the file. What does it mean and how to fix it?
RE: Can't save custom code on theme settings
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:
Hope this helps you.
Thanks!