Problem/Motivation

Let's assume the following scenario:

Expected result

It works as expected

Actual result

Fatal in \Drupal\views\Plugin\views\query\Sql::getCacheTags due to a method call on NULL.

Proposed resolution

The problem

\Drupal\views\Plugin\views\query\Sql::loadEntities treats revision IDs and entity IDs partially the same, see $flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE);. The current loading of revisions though works, because, a) Either we start with the revision base table, which let's us load all the revisions anyway or b) We start with the node table and join on the default revision, so we load the default entity always.

You can reproduce this with the attached view.
Note: Therefore you need a node with default revision ID = 4, but one revision with revision ID 5.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

dawehner created an issue. See original summary.

dawehner’s picture

Issue summary: View changes
StatusFileSize
new8.97 KB
dawehner’s picture

Issue summary: View changes
dawehner’s picture

Status: Active » Needs review
StatusFileSize
new3.92 KB

This is a prototype of a patch.

dawehner’s picture

StatusFileSize
new11.87 KB
new15.79 KB

This time with a test

The last submitted patch, 5: 2714989-fail.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 5: 2714989-5.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new15.93 KB
new1.43 KB

And a small bugfix

Status: Needs review » Needs work

The last submitted patch, 8: 2714989-6.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new15.95 KB
new535 bytes

There are some things, I simply don't understand.

jibran’s picture

StatusFileSize
new11.89 KB

Fixed the fail patch as well.

Status: Needs review » Needs work

The last submitted patch, 11: 2714989-fail.patch, failed testing.

jibran’s picture

Some minor feedback other that this is RTBC.

  1. +++ b/core/modules/views/src/Plugin/views/query/Sql.php
    @@ -1487,58 +1487,73 @@ public function loadEntities(&$results) {
    +          // Drupal core currently has no way to load multiple revisions. Sad.
    

    We can be little optimistic in comment.

  2. +++ b/core/modules/views/tests/src/Kernel/Plugin/SqlEntityLoadingTest.php
    @@ -0,0 +1,82 @@
    + * @see \Drupal\views\Plugin\views\query\Sql
    + * @group views
    

    Technically @see should be at the end of the doc block.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new15.95 KB
new1.32 KB

Thank you for the review!

We can be little optimistic in comment.

Well, I just moved things around ...

traviscarden’s picture

I tested the patch in #14, and it worked for me and fixed #2714717: Content (nid) relationship joins to wrong nodes. I'm probably not deep enough into this to RTBC it, though.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @dawehner for fixing feedback.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -1487,58 +1487,73 @@ public function loadEntities(&$results) {
+
+  protected function assignEntitiesToResult($ids, array $entities, array $results) {

Missing doc block.

I guess we can backport this to 8.1.x by adding an _ to assignEntitiesToResult() if we want.

Reviewing the code here is painful.

dawehner’s picture

@alexpott and myself agreed on adding unit tests for this quite complex piece of code.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new35.89 KB
new22.24 KB

Added some unit test coverage as well as documentation.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Seems perfect now.

alexpott’s picture

Version: 8.2.x-dev » 8.1.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed e888be3 and pushed to 8.2.x. Thanks!

@dawehner nice coverage. Using the _ method we can port this fix to 8.1.x

diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php
index 6f8ab15..cfe593c 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -5,7 +5,6 @@
 use Drupal\Component\Utility\NestedArray;
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Database\Database;
-use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\views\Plugin\views\display\DisplayPluginBase;

Unused use removed on commit.

  • alexpott committed e888be3 on 8.2.x
    Issue #2714989 by dawehner, jibran: Views which load the same entity...
jibran’s picture

Issue tags: +Novice

Seems like a novice task.

dawehner’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Novice
StatusFileSize
new35.86 KB
new2.68 KB

I'm sorry, I just did that

Status: Needs review » Needs work

The last submitted patch, 24: 2714989-24.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new35.86 KB
new1.59 KB

There we go

Status: Needs review » Needs work

The last submitted patch, 26: 2714989-26.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review

Its green again ...

kristiaanvandeneynde’s picture

Status: Needs review » Reviewed & tested by the community

It's green and it prefixes a new protected method with an underscore as mentioned in #2744157: [policy, no patch] Clarify the policy around backporting changes with potential issues. It changes nothing aside from that, so looks like a clean backport to me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed c6ed7c9 and pushed to 8.1.x. Thanks!

  • alexpott committed c6ed7c9 on 8.1.x
    Issue #2714989 by dawehner, jibran: Views which load the same entity...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Chris Gillis’s picture

I was having the same symptoms with a custom view in Drupal 8.3.7. I went through all the relations and marked them "required". This fixed the issue. Just noting this here for findability.

amaisano’s picture

Having this problem on 8.4.0. The view is using the content_revisions base table and has a relationship to both the moderation_state as well as the main node. Requiring the relationships did not resolve the issue as in the previous comment.

When deleting a revision, returning to this revisions view afterwards triggered the error.

I completely disabled caching on that display and the issue went away.

phillipHG’s picture

Having the same issue on 8.4.0. I tried disabling caching (caching: none in advanced view settings) as mentioned in #34, but still issue happens.