Notice: Undefined index: in file_ajax_upload() (line 258 of modules/file/file.module).

Created on 13 January 2012, over 12 years ago
Updated 11 April 2024, 2 months ago

I received the following error whenever I was creating a custom audio field.

Notice: Undefined index: in file_ajax_upload() (line 258 of modules/file/file.module).

Here is the code that the error is referring too:

// Get the current element and count the number of files.
  $current_element = $form;
  foreach ($form_parents as $parent) {
    $current_element = $current_element[$parent];
  }
  $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;

Here is the code to reproduce the error:
audio.install

<?php

/**
 * Implements hook_field_schema().
 */
function audio_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'description' => 'The mp3 {file_managed}.fid being referenced in this field.',
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'ogg_fid' => array(
        'description' => 'The ogg {file_managed}.fid being referenced in this field.',
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'uid' => array(
        'description' => "The {users}.uid being referenced in this field.",
       	'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'title' => array(
        'description' => "Audio title text",
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
      'desc' => array(
    		'description' => "Audio description text",
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
       ),
      'length' => array(
        'description' => 'The length of the audio in seconds.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
    ),
    'indexes' => array(
      'fid' => array('fid', 'ogg_fid'),
      'uid' => array('uid'),
    ),
    'foreign keys' => array(
      'fid' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid', 'ogg_fid' => 'fid'),
      ),
      'uid' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
  );
}

audio.module

<?php

/**
 * Implements hook_field_info().
 */
function audio_field_info() {
	return array(
		'audio' => array(
			'label' => t('Audio'),
			'description' => t('This field handles HTML5 Audio'),
			'default_widget' => 'audio_simple',
			'default_formatter' => 'audio_player',
			'settings' => array(
        'uri_scheme' => 'private',
      ),
      'instance_settings' => array(
        'file_extensions' => 'mp3',
        'file_directory' => 'audio/mp3',
      ),
		),
	);
}

/**
 * Implements hook_field_is_empty().
 */

function audio_field_is_empty($item, $field) {
	if ($field['type'] == 'audio') {
		return file_field_is_empty($item, $field);
	}
	return FALSE;
}

/**
 * Implements hook_field_widget_info().
 */
function audio_field_widget_info() {
	return array(
		'audio_simple' => array(
			'label' => t('Audio'),
			'description' => t('Add audio by file type.'),
			'field types' => array('audio'),
			'settings' => array(
        'progress_indicator' => 'throbber',
      ),
			'behaviors' => array(
				'multiple values' => FIELD_BEHAVIOR_DEFAULT,
				'default value' => FIELD_BEHAVIOR_DEFAULT,
			),
		),
	);
}

/**
 * Implements hook_field_widget_form().
 */
function audio_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
	
	if ($instance['widget']['type'] == 'audio_simple') {
	
		// Add display_field setting to field because file_field_widget_form() assumes it is set.
 		$field['settings']['display_field'] = 0;

		$elements = file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  	
  	foreach (element_children($elements) as $delta) {
  		// Add all extra functionality provided by the image widget.
    	$elements[$delta]['#process'][] = 'audio_field_widget_process';
  	}
  	
		if ($field['cardinality'] == 1) {
		  // If there's only one field, return it as delta 0.
		  if (empty($elements[0]['#default_value']['fid'])) {
		    $elements[0]['#description'] = theme('file_upload_help', array('description' => $instance['description'], 'upload_validators' => $elements[0]['#upload_validators']));
		  }
		} else {
			$elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators']));
		}
		
		$elements = $elements[0];
		unset($elements[0]);
		
	}
		
	return $elements;
	
}


/**
 * An element #process callback for the audio field type.
 *
 * Expands the audio type to include the title, description, and ogg file fields.
 */
function audio_field_widget_process($element, &$form_state, $form) {
	
	$item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];
  
  $instance = field_widget_instance($element, $form_state);
  
  $upload_validators = $element['#upload_validators'];
	$upload_validators['file_validate_extensions'] = array('ogg');
	
	$element['ogg_fid'] = array(
		'#name' => 'blah',
	  '#type' => 'managed_file',
	  '#title' => t('ogg'),
	  '#upload_location' => 'private://audio/ogg',
	  '#upload_validators' => array(
	  	'file_validate_extensions' => array('ogg'),
	  ),
	  '#default_value' => !empty($item['ogg_fid']) ? $item['ogg_fid'] : '',
	  '#access' => (bool) $item['fid'],
	  '#description' => empty($item['ogg_fid']) ? theme('file_upload_help', array('description' => $instance['description'], 'upload_validators' => $upload_validators)) : null,
	);
	
			
	$element['title'] = array(
		'#type' => 'textfield',
		'#title' => t('Title'),
		'#default_value' => isset($item['title']) ? $item['title'] : null,
		'#access' => (bool) $item['fid'],
	);
	
	return $element;
	
}

/**
 * Implements hook_field_presave().
 */
function audio_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {	
	list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  foreach ($items as $item) {
  	$fids = array(
  		$item['fid'],
  		$item['ogg_fid'],
  	);
  	foreach ($fids as $fid) {
	  	if (!empty($fid)) {
		    $file = file_load($fid);
		    if (!empty($file->status)) {
		      $file->status = FILE_STATUS_PERMANENT;
		      file_save($file);
		    }
		    if (!empty($file) && !empty($entity_type) && !empty($id)) {
		    	file_usage_add($file, 'audio', $entity_type, $id);
		    }
		  }
		 }
  }

}

If you enable the module, then create an "Audio" field on a node, then you will need to upload an mp3 file (which works fine), after that you will get the ogg_fid field, upload the ogg file and you will receive the error. However, even with the error, the field will save properly.
I noticed that for some reason all of the "ogg_fid" data is being saved with a child of "fid", (look at the name of the form element in the HTML), however, I couldn't find a way to remove this child (which I assume is what is causing the error).

I'm assuming the code could be changed to something like this:

// Get the current element and count the number of files.
  $current_element = $form;
  foreach ($form_parents as $parent) {
    if (!empty($current_element[$parent])) {
      $current_element = $current_element[$parent];
    }
  }
  $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;

However, I do not have enough experience with file.module to know if it will break something or not.

πŸ› Bug report
Status

Closed: outdated

Version

11.0 πŸ”₯

Component
File moduleΒ  β†’

Last updated 3 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States davidwbarratt

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024