File usage is increased when changing default language

Created on 26 September 2017, over 6 years ago
Updated 26 June 2023, 11 months ago

When trying to reproduce this issue: https://www.drupal.org/node/2810355 β†’ which is related in the sense that the file usage is also updated incorrectly when saving.

How to reproduce:

- Add content type with image field (ensure no revisions, since revisions do show the expected behaviour)
- Enable the local/language module (you don't need content translation)
- Add 2 languages
- Set the translation for the the content type
- Add a node with an image
- Change the language of the node

Expected:
file_usage is still at 1.

Actual:
file_usage is at 2 (and increments every time you switch language).

πŸ› Bug report
Status

Closed: cannot reproduce

Version

9.5

Component
File systemΒ  β†’

Last updated 1 day ago

Created by

πŸ‡§πŸ‡ͺBelgium suranga.gamage

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.

  • πŸ‡¦πŸ‡·Argentina ibonelli

    Working on this during DrupalCon 2023 Pittsburgh.

  • last update 12 months ago
    CI aborted
  • last update 12 months ago
    30,337 pass, 1 fail
  • Issue was unassigned.
  • πŸ‡¦πŸ‡·Argentina ibonelli

    I've been working on this today and I can confirm the problem. Even though I was able to run the test, they have an strange error which doesn't seem related to the particular problem.

    I thought of an experiment to see if the amount of languages had anything to do with the problem, but that does not seem to be the case. With 3 languages in the system (EN+ES+IT) I got a 2 usage instead of one for my article, and with 4 (EN+ES+IT+PT) I got the same (new article). So the 2 doesn't seem to be related to the amount of languages. Deleted and got back to 2 languages (EN+ES) and I still got a 2 usage count. The odd thing is that removing spanish and leaving only 1 language (EN) did not change the behavior, I still get a 2 usage count when I create a new article and add a picture to it.

    I guess the next step is some debugging to find where it is being added. But it is quite straight forward to test/debug in a local Unami install. Maybe even trying disabling the language and the translation modules (which doesn't seem that straight forward for Unami as it is a site install).

  • πŸ‡¦πŸ‡·Argentina ibonelli

    There is definitely something strange on this one. I tried one more time with a fresh install (no Unami SI). Tried all I could think of to reproduce, checked before adding the languages, nothing. I could not reproduce on a fresh install.

    Even though I came empty handed, this time I took the opportunity of debugging the internals. As far as I can tell the code resposible of this is core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php. The two parts I could identified as involved are:

    from line 21:

    public function postSave($update) {
      $entity = $this->getEntity();
    
      if (!$update) {
        // Add a new usage for newly uploaded files.
        foreach ($this->referencedEntities() as $file) {
          \Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
        }
      }
    

    And then at line 40:

    // On new revisions, all files are considered to be a new usage and no
    // deletion of previous file usages are necessary.
    if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) {
      foreach ($files as $file) {
        \Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
      }
      return;
    }
    

    To me next step would be to test this again with Unami and debug it as I did it this time. There is something else going on in Unami which triggers the bug.

  • πŸ‡¦πŸ‡·Argentina ibonelli

    I run the test in Unami, this time with debugging.

    First thing I noticed is the use of field_media_image in Unami, instead of the use of field_image in the basic install. So even though in both situations we are adding an image, in one case is a field and in the other an entity. I get the impression this is at least part of the reason why the issue doesn't express on a clean installation.

    And when adding an article I got the aforementioned issue: I get 2 uses for the image. But, this time I realized that I do not need to add a translation. As soon as I add the article (and without even saving it) I already see the file in the system being used twice. This happens because before saving the article node, I save the field_media_image. As soon as it gets saved, it already says it is used 2 times. And when I saved the article node, the usage remains the same (2 times).

    While debugging the add of the image to the field_media_image entity, I realized that the code mentioned on my last post gets called twice. Even though it has a for() statement within it, that only does one iteration each time. But the method postSave() gets called two times.

    I my head next step is to debug why it gets called twice. As soon as I'm able I'll get on with that and report what I find.

  • πŸ‡¦πŸ‡·Argentina ibonelli

    Today I followed a hunch I had. Instead of doing the code deep dive, I tried something new. Still using Unami I added a new field_image to the content type. Reasoning being, the field in Unami is a field_media_image entity and maybe the file it is being used 2 times: One by the node entity, and one by the media entity. I did try to debug that, but I will need to go a bit deeper to get to the bottom of that.

    In any case the testing was "successful". I do get 1 use for the file as a field_image and 2 uses for the field_media_image:

    My impression is that this is not a bug, but a working as design. And that the increase of use with the different languages is not the problem, but the increase of use with the field_media_image. But this is probably not a problem, but the result of the description above (1 use by the node entity and 1 use by the media entity). As I described before, adding languages doesn't change the usage. I couldn't reproduce on a fresh install with a field_image. I can only see the problem with Unami, which uses the field_media_image.

  • last update 11 months ago
    29,552 pass, 1 fail
  • Status changed to Closed: cannot reproduce 11 months ago
  • πŸ‡¦πŸ‡·Argentina ibonelli

    We discussed this in #bugsmash and we feel the problem is no longer there for 10.x & 11.x. We can not reproduce and it seems the usage count discrepancy is not related to the language system, but to the use of two different entities.

    If we put this together with the new core initiative #3364744 reliable entity-usage system to core 🌱 Add a reliable entity-usage system to core Active (thanks @Lendude for sharing this with me!) we should probably close this issue in favor of the new initiative. As described the issue is no longer there.

  • πŸ‡¦πŸ‡·Argentina ibonelli
Production build 0.69.0 2024