Missing file with ID 0. ImageItem.php

Created on 22 November 2022, over 1 year ago
Updated 22 June 2023, 12 months ago

Problem/Motivation

During migrations, I am getting hundreds of error messages like these:

  • [warning] Missing file with ID . ImageItem.php
  • [warning] Missing file with ID 0. ImageItem.php

Sometimes it is hundreds of one, sometimes hundreds of the other, and sometimes they are in pairs, one followed by the other hundreds of times over.

I am only seeing these with the ID being empty or 0. These are generated when migrating nodes that have an empty image field.

The errors are triggered by the trigger_error() in this:

  public function preSave() {
    parent::preSave();

    $width = $this->width;
    $height = $this->height;

    // Determine the dimensions if necessary.
    if ($this->entity && $this->entity instanceof EntityInterface) {
      if (empty($width) || empty($height)) {
        $image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
        if ($image->isValid()) {
          $this->width = $image->getWidth();
          $this->height = $image->getHeight();
        }
      }
    }
    else {
      trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING);
    }
  }

I don't think either empty or 0 is a valid ID, so this error message is not correct.

Proposed resolution

Use the existing error message when target_id is a non-zero integer. Otherwise, use a different message. (Or perhaps none at all; does empty or 0 mean there is no entity and this is not actually a problem?)

Remaining tasks

Decide what the different error message should be. Implement.

🐛 Bug report
Status

Needs work

Version

9.5

Component
Image system 

Last updated about 11 hours ago

Created by

🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    30,338 pass
  • 🇮🇳India venkatadapa

    Attached a patch to fix this issue. If the target_id is empty (i.e integer zero) it is irrelevant to show this error message.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & pgsql-10.12
    last update about 1 year ago
    30,338 pass
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    As a bug can we add a test assertion to show the issue.

    Thanks!

  • 🇺🇸United States mikelutz Michigan, USA

    Moving this issue to the image system since this is not directly related to migrations. Having a zero or null id here is a problem, since it means you are saving an image field with an invalid or missing target id, and this shouldn't happen. We definitely don't want to surpress the error in this case. For you, this likely means there is an error in your migration, causing the field to be created with an empty value instead of being skipped and having no value. Someone with in the #migration slack channel may be able to help you with that.

    Regardless, triggering this line with an invalid id can happen if a referred file is deleted. Getting this error with an id of 0 or null means there is a bigger problem happening somewhere. If this was happening in the normal drupal UI, it would mean a big big elsewhere that needs to be fixed, rather than changing the error message, but migrations are very powerful and can let you get into situations like this if not configured correctly, and if that's the case, there may be no need to adjust the error here, as it would be impossible to trigger in the UI due to other checks in the entity form system.

Production build 0.69.0 2024