Updated: Comment #42
Problem/Motivation
This bug is known to affect files, checkboxes, dates, boolean and probably other fields. Multistep forms are especially affected because any files (or field settings) added in previous steps will be lost when the form is finally submitted on the final step. This is because fields which are not in the active step are hidden with #access=false.
Steps to reproduce
1. Create a new content type with an image field and give it a default image.
2. Add a node of that content and upload an image different from the default.
3. Create a new module and in the .module file, add the following code (note, this module is named `devmodule`):
<?php
function devmodule_menu() {
$items['devmodule'] = array(
'page callback' => 'devmodule_callback',
'access callback' => TRUE,
);
return $items;
}
function devmodule_callback() {
module_load_include('inc', 'node', 'node.pages');
$node = node_load(1); // Use the nid of the node you created in Step #2.
$form = node_page_edit($node);
$form['field_image']['#access'] = FALSE; // 'field_image' is the name of the image field added in Step #1.
return render($form);
}
4. Enable the module and clear the cache.
5. Navigate to the menu path defined in the module (i.e. http://d8.local/devmodule) where you should see the node edit form without the image field.
7. Save the form.
8. Result: The image file is deleted and replaced by the default image.
Proposed resolution
The following code exists in file.module:
// Process any input and save new uploads.
if ($input !== FALSE) {
$return = $input;
// Uploads take priority over all other values.
if ($file = file_managed_file_save_upload($element)) {
$fid = $file->fid;
If file is #access=false, we have $input = NULL (which is !== false), thus, $fid is set to 0.
Changing
if ($input !== FALSE) {
to
if ($input !== FALSE && $input !== NULL) {
fixes the issue. No side effects were confirmed so far.
Remaining tasks
Determine if the test is adding new test coverage, if so add it.