You are here

function _filefield_file_form_description_reset in FileField 5.2

This after_build function is needed as fix for tricky Form API behaviour: When using filefield without AJAX uploading, the description field was not updated to a new '#default_value' because the textfield has been submitted, which causes Form API to override the '#default_value'.

That bug is fixed with this function by comparing the previous filename to the new one, and resetting the description to the '#default_value' if the filename has changed.

1 string reference to '_filefield_file_form_description_reset'
_filefield_file_form in ./filefield.module

File

./filefield.module, line 664
Defines a file field type.

Code

function _filefield_file_form_description_reset($form, $form_values) {

  // Don't bother resetting the description of files that stay the same
  if ($form['fid']['#value'] != 'upload') {
    return $form;
  }

  // Get the previous filename for comparison with the current one.
  $previous = $form['previous_filepath']['#post'];
  foreach ($form['previous_filepath']['#parents'] as $parent) {
    $previous = isset($previous[$parent]) ? $previous[$parent] : NULL;
  }

  // If a new file was uploaded (the file path changed), reset the description.
  if ($previous != $form['filepath']['#value']) {
    $form['description']['#value'] = $form['description']['#default_value'];
  }
  return $form;
}