Problem/Motivation

When viewing or editing a node with an attached image after deleting that image from admin/content/file, an message like Warning: array_key_exists() expects parameter 2 to be array, null given in theme_image_formatter() (line 605 of modules/image/image.field.inc). is generated.

Steps to reproduce in comment #5.

Proposed resolution

Check-for and handle this case so an error is not emitted.

Remaining tasks

Unknown.

User interface changes

None.

API changes

Unknown.

Original report by @MiroslavBanov

This error shows when an image is removed in files administration, but it has usage in nodes:
Warning: array_key_exists() expects parameter 2 to be array, null given in theme_image_formatter()
The problem is related to this issue, which solves similar problems for missing file attachments: https://drupal.org/node/2132383
I've attached a patch that tries to remove file field items for missing files.

Comments

miroslavbanov’s picture

Issue summary: View changes
miroslavbanov’s picture

Status: Active » Needs review
dave reid’s picture

Status: Needs review » Needs work

Can you give me more details on what field formatter and file type display configurations I should use to try and reproduce this? I feel that this could be fixed in a better place rather than having to run an array_filter().

miroslavbanov’s picture

Status: Needs work » Closed (cannot reproduce)

Not sure why this happens. I have a complex environment, with Workbench moderation, media_multiupload, and more. The problem happened after I delete a file (from admin/content/file) that is being used by a node. I had other notices as well, on non-image files too. And this patch doesn't even solve non-image files.

I'm closing for now, and will reopen if I figure out how to reproduce on normal environments, and what really causes this.

mparker17’s picture

Issue summary: View changes
Status: Closed (cannot reproduce) » Active

I have been able to reproduce this:

  1. Create a new Drupal 7 site: drush -y pm-download drupal-7.x ; drush -y site-install.
  2. Download and install the file_entity and media modules, version 7-x.2.x-dev: drush -y dl file_entity-7.x-2.x media-7.x-2.x.
  3. Log in as the super-administrator.
  4. Go to admin/structure/types and add a content type named Photo gallery.
  5. Go to admin/structure/types/manage/photo-gallery/fields and add an image field. Set the widget type to Media browser. In the field settings, enable the Upload and Library browser plugins.
  6. Go to node/add/photo-gallery and add a photo gallery node. Upload one or more images to the image field.
  7. Go to admin/content/file and delete one of the images you uploaded.
  8. Visit admin/reports/dblog. Note there are no errors in the log.
  9. View the node you created in step 6.
  10. Visit admin/reports/dblog. Note that a Warning: array_key_exists() expects parameter 2 to be array, null given in theme_image_formatter() message has been added to the recent log messages.
  11. Edit the node you created in step 6.
  12. Visit admin/reports/dblog. Note that another Warning: array_key_exists() expects parameter 2 to be array, null given in theme_image_formatter() message has been added to the recent log messages.
mparker17’s picture

Issue summary: View changes

Put link to steps to reproduce in issue summary.

miroslavbanov’s picture

This is the code that I use to fix it for myself in a custom module:

/**
 * Implements hook_field_attach_load().
 */
function mymodule_field_attach_load($entity_type, $entities, $age, $options) {
  // Loop over all the entities looking for attached files.
  foreach ($entities as $entity) {
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
    // Get every file field instance on this entity.
    $entity_instances = field_info_instances($entity_type, $bundle);
    $file_fields = array_filter(field_info_field_map(), 'mymodule_files_filter');
    // Try to remove missing or invalid files to prevent errors, warnings, etc.
    foreach (array_intersect_key($entity_instances, $file_fields) as $field_name => $instance) {
      if (!empty($entity->{$field_name})) {
        foreach ($entity->{$field_name} as $langcode => $items) {
          // Filter out missing files.
          $entity->{$field_name}[$langcode] = array_filter($items);
        }
        // Filter out empty language sets.
        $entity->{$field_name} = array_filter($entity->{$field_name});
      }
    }
  }
}

/**
 * Filter callback - get image and file fields.
 */
function mymodule_files_filter($field) {
  return $field['type'] == 'image' || $field['type'] == 'file';
}
mstrelan’s picture

Perhaps when files are deleted from admin/content/file there should be a warning that the file is associated with one or more entities, and if it's still confirmed to delete the file the entities should be updated appropriately.

dave reid’s picture

This is a duplicate of a core bug that image field does not handle deleted items properly: #920840: Broken images displayed and PHP notices when file/image field values are missing

varsharani’s picture

This warning showing me in drupal8.

Warning: array_key_exists() expects parameter 2 to be array, null given in Drupal\image\Plugin\Field\FieldWidget\ImageWidget::validateRequiredFields() (line 263 of core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php)

screon’s picture

Varsharani, I've got the same error. Did you manage to solve it? I'm still backtracing.

loopy1492’s picture

Can confirm that upgrading this module to the most recent version, then applying the core patch at #920840 should solve the issue.