Problem/Motivation

The views integration does not add the entity type ID to the join. Steps to reproduce:

  1. Add moderation to two different entity types.
  2. Create a moderation state for entity ID 1 of both types of entities.
  3. Create a view of either entity type.
  4. Add a relationship to the moderation entity.
  5. See two rows, one which follows the moderation status of the actual entity you are viewing and one with follows the moderation status of the other entity type.

Proposed resolution

Make this work.

Remaining tasks

Patch and test.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

Sam152 created an issue. See original summary.

sam152’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.04 KB

I can't seem to find any docs on the difference between extra and join_extra. The latter seems far less common in core and I can't actually see where join_extra actually impacts a query, outside one mention in reverse relationships.

Perhaps someone more familiar with views could validate this?

In any case current patch fixes my issue.

sam152’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: 2819477-content-moderation-join-2.patch, failed testing.

sam152’s picture

The test coverage existing leads me to believe there is something more complex going on in my use case than a simple mixup in keys.

sam152’s picture

Status: Needs work » Needs review
StatusFileSize
new9.54 KB
new9.55 KB
new932 bytes

I'm not confident #2 is in the right direction. Here is a failing and passing test with the entity type contained in #2812811: Use EntityPublishedInterface during moderation of entities to add support beyond nodes. There is an interdiff between the patches which should show that creating an unrelated entity fails the tests and none of the other test boilerplate interferers.

The last submitted patch, 6: 2819477-views-integration--failing-test-6.patch, failed testing.

timmillwood’s picture

In #2787881: Moderating a non-translatable entity type throws exception we are just editing the EntityTestWithBundle entity type to make it revisionable, which saves having to create a new entity type (and bundle).

dawehner’s picture

I'm super confused that https://www.drupal.org/files/issues/2819477-views-integration--passing-t... fixes the problem, given that it doens't really contain a fix?

  1. +++ b/core/modules/content_moderation/config/schema/content_moderation.schema.yml
    @@ -38,7 +38,7 @@ content_moderation.state_transition.*:
    -node.type.*.third_party.content_moderation:
    +content_moderation_third_party_settings:
    

    Nice!

  2. +++ b/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
    @@ -41,6 +45,18 @@ protected function setUp($import_test_views = TRUE) {
    +//    $test_entity = EntityTestRevWithBundle::create([
    +//      'type' => 'test_bundle',
    +//    ]);
    +//    $test_entity->moderation_state->target_id = 'draft';
    +//    $test_entity->save();
    

    Is this leftover code we can remove?

sam152’s picture

Both are the failing patch, just one has the inderdiff applied to demonstrate the specific part of the patch that was causing the fail. I haven't developed a patch that fixes all the test cases including the one contained in #6.

sam152’s picture

StatusFileSize
new6.77 KB

Status: Needs review » Needs work

The last submitted patch, 11: 2819477-11.patch, failed testing.

timmillwood’s picture

+++ b/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
@@ -41,6 +57,19 @@ protected function setUp($import_test_views = TRUE) {
+//    $test_entity->save();

I guess this shouldn't be commented out.

sam152’s picture

Status: Needs work » Needs review
StatusFileSize
new6.77 KB
new610 bytes

Good point, this should produce 2 fails.

sam152’s picture

StatusFileSize
new1.77 KB

I think the fails are issues with the test views more than anything else. They pass with the all of the current join conditions commented out.

Not sure how to proceed from here.

The last submitted patch, 14: 2819477-14.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 15: passing.patch, failed testing.

sam152’s picture

StatusFileSize
new1.71 KB
new3.01 KB

Decided to revisit this one and believe I've cracked it. There is a join based on revision IDs in one of the definitions, but in this case two base tables are being joined, which only ever have a single row for each entity. From what I can see this makes sense, but I'm also somewhat relying on the test coverage to guide this change, as it's still quite a complex part of the module.

Requires code from #2812811: Use EntityPublishedInterface during moderation of entities to add support beyond nodes, will set to needs review after that is in.

sam152’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB
new3.7 KB
new2.4 KB

Found a bug in the tests, it was checking the node in the test against the revision ids for the ContentModerationState entity instead of the node entity. Fixed here.

If this passes, I think it's worth another review.

Status: Needs review » Needs work

The last submitted patch, 19: 2819477-19-test-only.patch, failed testing.

sam152’s picture

Status: Needs work » Needs review

The last submitted patch, 18: 2819477-18-test-only.patch, failed testing.

The last submitted patch, 18: 2819477-18.patch, failed testing.

dawehner’s picture

+++ b/core/modules/content_moderation/src/ViewsData.php
@@ -193,15 +193,11 @@ public function getViewsData() {
             ],
-            [
-              'field' => 'content_entity_revision_id',
-              'left_field' => $entity_type->getKey('revision'),
-            ],

Ah that makes sense, thank you for the explanation.

timmillwood’s picture

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.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

sam152’s picture

I wonder if this should be ported to workbench_moderation too?

xjm’s picture

Priority: Normal » Major
Status: Reviewed & tested by the community » Needs review

So like. The view will show totally wrong data? That's a neat "feature". ;) Sounds major to me.

+++ b/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
@@ -44,7 +48,13 @@ protected function setUp($import_test_views = TRUE) {
+    // Create a totally unrelated entity to ensure the extra join information
+    // joins by the correct entity type.
+    $entity = EntityTestMulRevPub::create([]);
+    $entity->save();

I don't know that this is actually enough to ensure that the node and the test entity have the same serial ID. Sometimes serial IDs in tests are different from what one expects. At the least, I think we should add an assertion that the two entities' IDs are the same, but there might be some other trick in some test somewhere for ensuring they have the serial IDs we expect. (Yes that's very helpful and specific, I know.)

xjm’s picture

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

Technically this fix could disrupt a site relying on the "feature" that the view shows them additional rows, but since Content Moderation is also alpha, I think this can go into any patch release or the beta.

jibran’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests +Needs upgrade path

I don't know that this is actually enough to ensure that the node and the test entity have the same serial ID. Sometimes serial IDs in tests are different from what one expects. At the least, I think we should add an assertion that the two entities' IDs are the same, but there might be some other trick in some test somewhere for ensuring they have the serial IDs we expect

Great! idea @xjm. result rows will have all the entities so we can add asserts on that.
I found another point.

+++ b/core/modules/content_moderation/src/ViewsData.php
@@ -193,15 +193,11 @@ public function getViewsData() {
-          'join_extra' => [
+          'extra' => [

@@ -216,7 +212,7 @@ public function getViewsData() {
-          'join_extra' => [
+          'extra' => [

This is a views data change so we at least need an update hook to clear the cache. A post-update hook will do ;-)

sam152’s picture

Status: Needs work » Needs review
Issue tags: -Needs upgrade path
StatusFileSize
new1.37 KB
new2.74 KB
new4.04 KB

Updated to assert the IDs are the same. Setting the ID of the test entity also works, so may as well ensure it matches the node.

The module is experimental and doesn't need an upgrade path.

sam152’s picture

Re: #30, having come across this in a real world scenario, my reaction was "eek, why do my views keep going nuts" more than "what a neat feature" ;-)

Status: Needs review » Needs work

The last submitted patch, 32: 2819477-32.patch, failed testing.

sam152’s picture

Status: Needs work » Needs review
sam152’s picture

Issue tags: +Workflow Initiative
sam152’s picture

This was so close. Any other takers on a review? :)

benjy’s picture

Status: Needs review » Reviewed & tested by the community

Would be good to have this in.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed d66623e to 8.4.x and c109192 to 8.3.x. Thanks!

diff --git a/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php b/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
index 8703d1f..d4c4aa3 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ViewsDataIntegrationTest.php
@@ -2,7 +2,6 @@
 
 namespace Drupal\Tests\content_moderation\Kernel;
 
-use Drupal\entity_test\Entity\EntityTestMul;
 use Drupal\entity_test\Entity\EntityTestMulRevPub;
 use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;

unused use fix on commit.

  • alexpott committed d66623e on 8.4.x
    Issue #2819477 by Sam152, timmillwood, dawehner, jibran, xjm: Views...

  • alexpott committed c109192 on 8.3.x
    Issue #2819477 by Sam152, timmillwood, dawehner, jibran, xjm: Views...

Status: Fixed » Closed (fixed)

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