Problem/Motivation

When you have more than one usage for a file, in the list of usages of this file (the page_2 of views.view.files.yml), each usage is listed the same amount of times as results are. As the view is doing a join between file_managed and file_usage and then again another join with file_managed with the result.

Steps to reproduce

Here is some code to reproduce the issue (if you use the same file in 2 nodes, you will have 4 rows on the file usage page; if you use the same file in 3 nodes, you will have 9 rows on the file usage page; if you use the same file in 4 nodes, you will have 16 rows on the file usage page, etc.):

$file = \Drupal\file\Entity\File::create([
  'uri' => 'public://test.png',
]);
$file->save();
$node = \Drupal\node\Entity\Node::create([
  'type' => 'article',
  'title' => 'Test 1',
  'field_image' => ['target_id' => $file->id()],
]);
$node->save();
$node = \Drupal\node\Entity\Node::create([
  'type' => 'article',
  'title' => 'Test 2',
  'field_image' => ['target_id' => $file->id()],
]);
$node->save();
drupal_flush_all_caches();

Proposed resolution

This is due to a double join between file_managed and file_usage, remove the duplicated join.
We should remove the following duplicate join of file_managed from the FileViewsData.php

$data['file_usage']['table']['join'] = [
      'file_managed' => [
        'field' => 'fid',
        'left_field' => 'fid',
 ],

Remaining tasks

  1. Needs review

User interface changes

N/A

API changes

TBD

Data model changes

N/A

Release notes snippet

N/A

CommentFileSizeAuthor
#80 interdiff-2632372-75-80.txt544 bytesmohit_aghera
#80 2632372-80.patch16.92 KBmohit_aghera
#75 interdiff-2632372-73-75.txt6.33 KBmohit_aghera
#75 2632372-75.patch16.39 KBmohit_aghera
#73 interdiff-2632372-67-73.txt6.5 KBmohit_aghera
#73 2632372-73.patch10.06 KBmohit_aghera
#67 interdiff-2632372-63-67.txt1.58 KBmohit_aghera
#67 2632372-67.patch3.56 KBmohit_aghera
#63 test-only-2632372-63.patch1.29 KBmohit_aghera
#63 interdiff-2632372-59-63.txt2.88 KBmohit_aghera
#63 2632372-63.patch1.98 KBmohit_aghera
#59 2632372-59.patch2.27 KBameymudras
#45 repeated_file_usages-2632372-45-8_6_x.patch3.34 KBedurenye
#45 repeated_file_usages-2632372-45-8_5_x.patch2.44 KBedurenye
#41 repeated_file_usages-2632372-41.patch2.48 KBjcnventura
#30 interdiff-repeated_file_usages-2632372-24-30-8_1_x.txt718 bytesedurenye
#30 repeated_file_usages-2632372-30-8_1_x.patch2.45 KBedurenye
#30 repeated_file_usages-2632372-30-8_2_x.patch3.35 KBedurenye
#24 interdiff-repeated_file_usages-2632372-16-24-8_0_x.txt565 bytesedurenye
#24 repeated_file_usages-2632372-24-8_0_x.patch2.29 KBedurenye
#17 interdiff-repeated_file_usages-2632372-8-16.txt1.82 KBedurenye
#17 repeated_file_usages-2632372-16-8_0_x.patch2.29 KBedurenye
#17 repeated_file_usages-2632372-16-8_1_x.patch3.35 KBedurenye
#17 repeated_file_usages-2632372-16-test_only.patch1.08 KBedurenye
#16 interdiff-repeated_file_usages-2632372-8-16.patch1.82 KBedurenye
#16 repeated_file_usages-2632372-16-8_0_x.patch2.29 KBedurenye
#16 repeated_file_usages-2632372-16-8_1_x.patch3.35 KBedurenye
#16 repeated_file_usages-2632372-16-test_only.patch1.08 KBedurenye
#9 AFTER repeated_file_usages-2632372-8.patch.png51.28 KBno_angel
#9 BEFORE repeated_file_usages-2632372-8.patch.png58.39 KBno_angel
#8 repeated_file_usages-2632372-8.patch486 bytesedurenye
#4 interdiff-repeated_file_usages-2632372-2-4.txt1.58 KBedurenye
#4 repeated_file_usages-2632372-4.patch2.27 KBedurenye
#2 repeated_file_usages-2632372-2.patch710 bytesedurenye
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

edurenye created an issue. See original summary.

edurenye’s picture

Status: Active » Needs review
FileSize
710 bytes

Deleted the unneeded join.

Status: Needs review » Needs work

The last submitted patch, 2: repeated_file_usages-2632372-2.patch, failed testing.

edurenye’s picture

Fixed the test fails.

no_angel’s picture

Can you please provide the steps to reproduce the issue?

Also should this be tested on the latest version of D8? 8.0.1

edurenye’s picture

I found it working on this issue in monitoring module #2614000: Add aggregated table verbose output of image derivative creation sensor, I basicaly added the file usages programaticaly, and when I checked the file usage list I foud that the results where repeated as many times as results we had.
I checked the sql query generated by the view and I could see that was doing a join between file_managed and file_usage and then again another join with file_managed with the result. So to fix it I just removed the las join.
I'm working always with the 8.0.x-dev branch of drupal so it happens in the 8.0.1
So to reproduce just add more than one file_usage for one file and check the view with the list of usages.

edurenye’s picture

Issue summary: View changes
edurenye’s picture

So here is the patch that adds aggregation, for 8.0.x

no_angel’s picture

Steps to reproduce the issue reported:

:

  1. simplytest.me + devel module
  2. generate 50 nodes of content for article
  3. https://rmnoc.ply.st/admin/content/files/usage/3

After patch steps :

  1. simplytest.me + repeated_file_usages-2632372-8.patch + devel module
  2. generate 50 nodes of content for article
  3. https://rmnon.ply.st/admin/content/files/usage/1
no_angel’s picture

Issue summary: View changes
no_angel’s picture

Status: Needs review » Reviewed & tested by the community

Changed status to RTBC

no_angel’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

I think we should improve Drupal\file\Tests\FileListingTest to test for this.

Also I think we need to consider an upgrade path here.

no_angel’s picture

Assigned: Unassigned » no_angel

I'd like to give #13 ago if there is some time for researching how.

no_angel’s picture

Version: 8.0.x-dev » 8.1.x-dev
Assigned: no_angel » Unassigned
Issue summary: View changes
Issue tags: +D8 upgrade path

Too big for me.
Issue summary updated.

edurenye’s picture

Added the upgrade path, finally I learned how to do it :)
Added tests.
I added 8_0_x and 8_1_x to the file names showing the branch they are for.

The last submitted patch, 16: repeated_file_usages-2632372-16-test_only.patch, failed testing.

The last submitted patch, 16: repeated_file_usages-2632372-16-8_0_x.patch, failed testing.

The last submitted patch, 16: interdiff-repeated_file_usages-2632372-8-16.patch, failed testing.

The last submitted patch, 17: repeated_file_usages-2632372-16-test_only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 17: repeated_file_usages-2632372-16-8_0_x.patch, failed testing.

The last submitted patch, 17: repeated_file_usages-2632372-16-8_0_x.patch, failed testing.

edurenye’s picture

Fixed the test fails in 8.0.x

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

edurenye’s picture

Version: 8.2.x-dev » 8.1.x-dev

I think we can still commit the patch for drupal 8.1 to 8.1 and 8.2.

edurenye’s picture

Version: 8.1.x-dev » 8.2.x-dev

Changed to 8.2.x

edurenye’s picture

Issue tags: -Needs tests, -D8 upgrade path
Berdir’s picture

Status: Needs review » Needs work
+++ b/core/modules/file/file.install
@@ -120,3 +120,17 @@ function file_requirements($phase) {
+function file_update_8001() {
+  // core/modules/file/config/optional/views.view.files.yml was changed in
+  // https://www.drupal.org/node/2632372 to change group_by to true.
+  $config_factory = \Drupal::configFactory();
+  $view = $config_factory->getEditable('views.view.files');
+  $displays = $view->get('display');
+  $displays['page_2']['display_options']['group_by'] = TRUE;
+  $view->set('display', $displays);
+  $view->save(TRUE);

Lets make this a bit more defensive. Check if the view exists, check if the setting you want to check exists and is actually set to FALSE right now.

If not, do not change/save the view.

This is default configuration, users might have deleted or changed it.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jcnventura’s picture

Version: 8.3.x-dev » 8.2.x-dev
Status: Needs review » Needs work

This is neither a "new development" nor a "disruptive change".. We should not wait for 8.3.x to fix this.

Maybe it's just lack of information on my part, but looking at http://cgit.drupalcode.org/drupal/tree/core/modules/file/config/optional..., I still see that 8.2.x has that group_by still set to false. If we change it to true on the 8.1.x patch, shouldn't we do the same in the 8.2.x patch? And also add the file_update_8001() function? There will be people upgrading 8.1.x sites to 8.2.x sites, after all.

edurenye’s picture

Status: Needs work » Needs review

No, with the patch for 8.2.x we use the relationship and it uses less resources than with the group_by, while in the 8.1.x we use the group_by as we though that the other change was to big for it.

jcnventura’s picture

edurenye’s picture

Indeed, my answer was wrong, you are right.
I solved this issue long time ago and I replied too fast, sorry.
With the RC we can still put it in? Or we have to do as before, one for 8.3 and one for 8.2?

jcnventura’s picture

Version: 8.2.x-dev » 8.3.x-dev

At this point, the patch should be applied to 8.3.x, and then patched to 8.2.x.

Let's see if the testbots are ok with it.

jcnventura’s picture

Status: Needs review » Reviewed & tested by the community

Testbots are OK, and so is my test on a real system..

This is definitively RTBC in 8.3.x and 8.2.x (same patch applies in both at this time).

catch’s picture

Component: file system » file.module
Status: Reviewed & tested by the community » Needs review

What happens if there are views out there relying on the automatic join?

jcnventura’s picture

Status: Needs review » Needs work

Indeed. It may be safer, and way more appropriate to use the solution in the 8.1.x patch.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jcnventura’s picture

Re-rolled the 8.1 patch in #30 for Drupal 8.4. This uses only the aggregation and then runs file_update_8001 to update the config for existing views.

No interdiff, as it is identical to #30.

edurenye’s picture

The real good solution was the other patch, I'm retesting that one to see if needs re-roll, your patch must focus on 8.3 not 8.4, but maybe seeing the time that this issue is been open we might just provide the good solution for 8.4, what do you think?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

edurenye’s picture

The last submitted patch, 45: repeated_file_usages-2632372-45-8_5_x.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 45: repeated_file_usages-2632372-45-8_6_x.patch, failed testing. View results

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue summary: View changes
Issue tags: +Bug Smash Initiative

I tested on Drupal 10.1.x, standard install and this is still relevant.

have closed #2560937: Multiple file usage records as a duplicate and adding credit.

ameymudras’s picture

Status: Needs work » Needs review
FileSize
2.27 KB

Still needs to write tests

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs steps to reproduce, +Needs Review Queue Initiative

This could use steps to reproduce. Not sure I'm seeing an issue.

Also as mentioned needs tests.

mfb’s picture

Issue summary: View changes
Issue tags: -Needs steps to reproduce +Needs tests

A while back I added reproduce code to the duplicate issue at #2560937: Multiple file usage records, now copying it to this issue

smustgrave’s picture

Thanks @mfb confirmed I see the issue now.

mohit_aghera’s picture

There was a discussion around two approaches in the issue. One patch was for 8.1.x and another one was for 8.2.x

The one that was for 8.1.x #24 #30 #45 was updating a views configuration and adding aggregation.
I think in #42it was discussed that this is not the good approach. I quite agree with that.
It is good to remove un-necessary join rather than adding aggregation.

Now patches for 8.2,x, 8.6.x mentioned in #30and #45 comes to picture. I feel that this is the correct approach.
Prefereed solution:
Remove the additional join happening in hook_views_data of the FileViewsData.php class.

The test cases added in #45 seem a bit confusing, so I've removed that test case and added a new simple one to validate the error. Attached test-only patch as well. Since we are not updating any views this time, I think we won't need any update hook or upgrade path test.
I've updated issue summary accordingly focussing approach #2 which is about removing additional join in the hook_views_data.

#59 re-roll is missing a couple of changes mentioned in the #45
Reportedly FileUsageTest.php file's changes are not included in the re-roll.

mohit_aghera’s picture

Status: Needs work » Needs review

The last submitted patch, 63: 2632372-63.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 63: test-only-2632372-63.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
3.56 KB
1.58 KB

Opps, putting back the views related changes.
Didn't realised that those changes might break the view's functional tests.
Not uploading the test only patch since test-only patch is failing for the new test case only.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Confirmed the issue using the snippet in the issue summary and the patch in #67 solves the issue.
#63 shows the tests fail as expected.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 67: 2632372-67.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review

I think previous failures were un-related.
Triggering run again for 11.x
Let's see how goes 🤞

Status: Needs review » Needs work

The last submitted patch, 67: 2632372-67.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
10.06 KB
6.5 KB

Tried to fix the issue.
To fix the issue, I had to update the view.
Which leads me to think that can there be any regression on the existing views because of the change?
Do we need to consider any upgrade path here?

+++ b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_node.yml
@@ -187,25 +132,90 @@ display:
+        fid:
+          id: fid
+          table: file_managed
+          field: fid
+          relationship: none
+          group_type: group
+          admin_label: 'File usage'
+          entity_type: file
+          entity_field: fid
+          plugin_id: standard
+          required: true
+        file_to_node:
+          id: file_to_node
           table: file_usage

I had to add this whole relationship.
Which makes me think do we need to consider any upgrade path here?

Looking forward to see opinions from other folks.

Status: Needs review » Needs work

The last submitted patch, 73: 2632372-73.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
16.39 KB
6.33 KB

Updating another view which was causing issues in relationship.
Hiding the patch in #73.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

This was previously RTBC and since the update needed was for a test view think it's safe to go back to RTBC!

jcnventura’s picture

Re: #73, an upgrade path should not be necessary as long as the views being changed are only used for testing. At the moment, there are no changes to any views config, so an upgrade path is not necessary.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

I manually tested this, and it worked as designed, without any updates to existing config.

It did however require a cache clear to work.

Should we add an empty post update hook to trigger the cache invalidation that resets the views config?

Or should we leave it as is knowing that the missing cache clear doesn't break anything, its just the bug doesn't get fixed until it is cleared

smustgrave’s picture

Status: Needs review » Needs work

If the standard is to do an empty post_update hook think it wouldn't be terrible to add here.

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
16.92 KB
544 bytes

Added an empty post_update hook.

Status: Needs review » Needs work

The last submitted patch, 80: 2632372-80.patch, failed testing. View results

mfb’s picture

There is a literally random failure on this patch, but it seems to work. I just don't understand the "why": Removing the built-in relationship between file_usage and file_managed (which seems like a useful thing to define) only to add this relationship back in the view itself?

If it really does make sense to remove this from the views data, then the above comment should be updated too:

Provide field-type-things to several base tables; on the core files table ("file_managed") so that we can create relationships from files to entities, and then on each core entity type base table so that we can provide general relationships between entities and files.

Removing this from views data seems problematic if every custom view that relied on it needs to be updated, so it presumably would need a change record?

By the way, I noticed that media was never added here as one of the core entity type base tables..