File(s) silently deleted when #access=false

Created on 1 July 2011, almost 13 years ago
Updated 31 May 2023, about 1 year ago

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.

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Needs work

Version

9.5

Component
File module  →

Last updated 3 days ago

Created by

🇩🇪Germany geek-merlin Freiburg, Germany

Live updates comments and jobs are added and updated live.
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.

  • 🇳🇿New Zealand quietone New Zealand

    This was a bugsmash target yesterday. Lendude and myself discussed this.

    The last report that this problem was occurring and the patch fixed it was in #47, 9 years ago. Then there was a break for 2 years. After that,
    in #53 a patch was made to reproduce the problem. That patch passed tests and it was reported that the problem could not be reproduced. The same comment suggest the test could be added. However, if the problem not longer exists, perhaps there is an existing test and this one does not need to be added. Since then there has been no activity.

    What remains is to determine if the test is worth adding. I have updated the issue summary.

    FWIW, I updated the patch and yes it passes.

  • 🇳🇿New Zealand quietone New Zealand

    And no longer a bug report.

  • 🇮🇳India pooja saraah Chennai

    Fixed failed commands on #81
    Attached patch against Drupal 9.5.x

  • 🇱🇺Luxembourg B-Prod

    @quietone I don't think this issue is definitively closed, as this bug still appear if you set the access to FALSE on a parent element of the file managed element, but not on the file managed element itself.

Production build 0.69.0 2024