Skip to content

file_managed_file_browser_open() throws PHP warnings when the managed_file element has no #field_name #7178

Description

@robertgarrigos

Summary

file_managed_file_browser_open() in core/modules/file/file.module assumes every managed_file form element belongs to a Field API field, and unconditionally reads $element['#field_name']. Elements built outside Field API (e.g. a custom block form using #type => 'managed_file' directly) never set #field_name, so this line breaks any time the file browser dialog is opened on such an element — regardless of whether a file is currently assigned to it or not.

What made me hit this bug in practice: file_managed_file_pre_render() only hides the "Select existing file" button while the element has a valid fid. My Hero block's assigned file became unavailable (the fid no longer resolved to a real file), so that button — normally hidden — reappeared. Clicking it for the first time is what exposed the pre-existing #field_name bug described above.

Steps to reproduce

  1. Create a Hero block and assign an image to it via the managed_file field. Save the block. While a valid file is assigned, the "Select existing file" / upload button stays hidden (file_managed_file_pre_render() sets #access = FALSE on it whenever #value['fid'] is not empty).
  2. The underlying file becomes unavailable — in my case the assigned file disappeared from the site (file record and/or physical file gone; exact cause still under investigation), leaving the block referencing a fid that no longer resolves to a valid file.
  3. Edit the Hero block again. Because the field's value is now effectively empty, Backdrop shows the "Select existing file" / upload button again (the condition in step 1 no longer holds).
  4. Click "Select existing file" to open the file browser modal.
  5. The two warnings below are logged, and only then does the dialog open.

Actual behavior

Two warnings are logged:

Warning: Undefined array key "#field_name" a file_managed_file_browser_open() (línia 1870 de /Users/robert/Sites/backdrop/core/modules/file/file.module).
Warning: Trying to access array offset on null a file_managed_file_browser_open() (línia 1871 de /Users/robert/Sites/backdrop/core/modules/file/file.module).

(the exact lines are the two below.)

$field = field_info_field($element['#field_name']);
$field_cardinality = $field['cardinality'];

$element['#field_name'] is undefined, so field_info_field(NULL) is called and returns NULL, and the following line then tries to read ['cardinality'] off NULL.

Expected behavior

No PHP warnings. managed_file elements not attached to a Field API field should default to single-file behavior ($field_cardinality = 1) instead of assuming #field_name is always present.

Proposed fix

$field_cardinality = 1;
if (!empty($element['#field_name'])) {
  $field = field_info_field($element['#field_name']);
  if (!empty($field['cardinality'])) {
    $field_cardinality = $field['cardinality'];
  }
}

Environment

  • Backdrop CMS version: 1.34.3
  • PHP version: 8.3

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions