I have been working on this code and I am having some errors, and I was wondering if anybody can find errors in this code... I have posted my error below it...

function background_audio_form($form, &$form_state) {

$form['background_audio_file']['file'] = array(
  '#type' => 'managed_file',
  '#title' => t('File'),
  '#description' => t('Allowed extensions: mp3'),
  '#upload_location' => 'public://MP3/',
  '#default_value' => variable_get('background_audio_file'),
  '#upload_validators' => array(
    'file_validate_extensions' => array('gif png jpg jpeg mp3'),

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save Changes'),

  return $form;

function background_audio_form_submit($form, $form_state) {

 // The file is automatically uploaded and saved in the default
 // validation process so you just need to load the file object at this point,
 // mark that you want to keep it, and save it again.
 // The file ID is contained in the $form_state['values'] array
 $file = file_load($form_state['values']['background_audio_file']);
 $file->status = FILE_STATUS_PERMANENT;

 $fid = $form_state['values']['background_audio_file'];

 $fileid = file_load($fid);

 variable_set('background_audio_upload_path', $fileid->uri);

Notice: Undefined index: background_audio_file in background_audio_form_submit() (line 107 of /Users/jtharpe/Overflow_Dev/sites/all/modules/background_audio/background_audio.module).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /Users/jtharpe/Overflow_Dev/includes/entity.inc).
Notice: Undefined property: stdClass::$uri in file_save() (line 573 of /Users/jtharpe/Overflow_Dev/includes/file.inc).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1329961119 ) in drupal_write_record() (line 6975 of /Users/jtharpe/Overflow_Dev/includes/common.inc).
  • What version of Drupal are you using? – acouch Feb 23 '12 at 4:24
  • @acouch as you can see in the warning line, D7 only has DrupalDefaultEntityController object. There is no such kind of object in D6. – Sithu Feb 23 '12 at 7:52

In your $form FAPI usage you called the file key file, a child element of the parent container background_audio_file. In your _submit() function you are not looking for file.

Do a Devel dpm($form_state['values']) or a print_r($form_state['values']) and you will see the key file with your content. Besides that your code looks ok.

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.