Problem/Motivation

In \Drupal\content_moderation\EntityOperations::updateOrCreateFromEntity we update or create a ContentModerationState entity, and add a translation to match the translation of the moderated entity. This however fails if the moderated entity is not translatable.

The same issue is in \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::getModerationState where we try to return the correct translation.

Proposed resolution

Check if the entity type is translatable.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

timmillwood created an issue. See original summary.

timmillwood’s picture

Issue summary: View changes
timmillwood’s picture

Status: Active » Needs review
StatusFileSize
new3.03 KB
timmillwood’s picture

Assigned: timmillwood » Unassigned
Issue tags: +Contributed project blocker

Marking this as a contrib blocker because Workspaces (as defined by Multiversion module) are not translatable, but we want them to be moderated. This currently works fine with Workbench Moderation, but doesn't with Content Moderation.

Status: Needs review » Needs work

The last submitted patch, 3: moderating_a-2787881-3.patch, failed testing.

dawehner’s picture

I'm wondering whether we could somehow test content_moderation with all kind of entity_test entities, which should give us test coverage for all kind of edge cases.

timmillwood’s picture

Status: Needs work » Needs review

@dawehner an entity type can only be moderated if it has a bundle and is revisionable. Therefore I'm not sure if entity_test entity types will work because they don't have bundles. However we could look at refactoring Content Moderation to not depend on bundles. The only reason it currently requires bundles is because this is where the moderation settings are stored.

dawehner’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

The last submitted patch, 3: moderating_a-2787881-3.patch, failed testing.

The last submitted patch, 3: moderating_a-2787881-3.patch, failed testing.

timmillwood’s picture

Not sure how best to tackle the "Needs tests" tag on this.

I'd like to address it in a follow up, either:
- Update content moderation to allow moderation on entity types without bundles. This could be done by either having moderation settings in the UI on the bundle as it is now and / or on the entity type via the annotation or hook_entity_type_alter.
- Change entity_test to have bundles.

timmillwood’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new9.26 KB
new7.82 KB
new4.49 KB

Adding tests... but annoyingly, the test_only.patch passes locally too, when it shouldn't.

That can wait until tomorrow.

Ended up adding an EntityTestRevType bundle for EntityTestRev to use.

Status: Needs review » Needs work

The last submitted patch, 12: test_only.patch, failed testing.

The last submitted patch, 12: moderating_a-2787881-12.patch, failed testing.

dawehner’s picture

I'm glad you don't have the problem that the tests aren't passing on drupal.org :P

timmillwood’s picture

Status: Needs work » Needs review
StatusFileSize
new11.42 KB
new11.3 KB
new3.4 KB

ok, lets hope for better luck this time.

Even though entity_test_rev is not translatable it still has a langcode, so removed that.
entity_test.install was adding some fields, but we needed a bundle first, so generating that too.

The last submitted patch, 16: test_only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 16: moderating_a-2787881-16.patch, failed testing.

timmillwood’s picture

Status: Needs work » Needs review
StatusFileSize
new12.9 KB
new1.59 KB

Fixing some of the failing tests.

Any suggestions welcome on the other one.s

Status: Needs review » Needs work

The last submitted patch, 19: moderating_a-2787881-19.patch, failed testing.

timmillwood’s picture

StatusFileSize
new7.95 KB

Even though it won't pass, uploading a patch as I won't be looking at this again until Tuesday.

On @amateescu's suggestion I've been reworking to use EntityTestWithBundle entity, rather than trying to add a bundle to EntityTestRev. The issue is EntityTestWithBundle is not revisionable, so trying to make it revisionable by entity_test_entity_type_alter. However I'm struggling to get this to fire before content_moderation_entity_type_alter fires, because this is where the moderation handler gets added, which is used to make an entity type moderated.

Interesting and fun suggestions welcomed.

timmillwood’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 21: moderating_a-2787881-21.patch, failed testing.

timmillwood’s picture

Status: Needs work » Needs review
StatusFileSize
new7.29 KB
new2.4 KB

Making EntityTestWithBundle revisionable.

Wonder if it breaks anything.

amateescu’s picture

StatusFileSize
new6.64 KB
new9.7 KB
new4.59 KB

Following up on #21, all we need to do in order to get entity_test_entity_type_alter() to fire before content_moderation_entity_type_alter() is to implement hook_module_implements_alter() and move 'entity_test' to the beginning of the array.

Here's a patch that does that and also cleans up the test a little. Interdiff is to #21.

The last submitted patch, 25: 2787881-25-test-only.patch, failed testing.

timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @amateescu!

Looks good to me!

xjm’s picture

Issue tags: +rc eligible

This is rc eligible as an experimental module improvement, per: https://www.drupal.org/core/d8-allowed-changes#rc

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

The rtbc in #27 is not really right - much of the work in the patch is @timmillwood's. The patch looks good to me but committing from this state sets a bad precedent.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -201,11 +200,14 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
+      if ($content_moderation_state->language()->getId() !== $entity_langcode) {
+        $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
+      }

Just a question for a follow up: Is it worth to have this additional if here in the first place? Why not get always the translation. All objects are available anyway.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestWithBundle.php
@@ -32,7 +32,6 @@
- *     "langcode" = "langcode",

I wonder if this could break contrib tests that use EntityTestWithBundle? Making this test entity non translatable seems like an unexpected change for this issue - how about we just make a new type?

sam152’s picture

#2812811: Use EntityPublishedInterface during moderation of entities to add support beyond nodes introduces a few new test entities. Maybe langcode can be removed from EntityTestRevWithBundle there?

timmillwood’s picture

There is no translatable = TRUE annotation in EntityTestWithBundle, so in theory it's not translatable, it just has a langcode entity key.

Although the issue this patch is trying to fix doesn't apply on entity types with langcode entity keys, so had to remove it to make this fail.

I'm all for making EntityTestWithBundleNonMul, but if we do that then maybe we should make EntityTestWithBundle actually translatable.

amateescu’s picture

It seems weird that some entity types which are not translatable define the 'langcode' key, so I opened #2818379: Remove the 'langcode' entity key for non-translatable entity types to discuss that.

amateescu’s picture

I discussed the issue mentioned above with @Berdir and it seems there is a valid use-case for a non-translatable entity type to define the 'langcode' key, which makes me think that the patch is not actually correct, in the sense that we should check the availability of the langcode field instead of whether the entity type is translatable or not.

And, given the above, we do need to provide a new test entity type for this :)

timmillwood’s picture

The biggest issue with creating a new entity type for this is we need a new bundle type too.

Working on a patch now.

timmillwood’s picture

StatusFileSize
new9.19 KB
new1.71 KB

We don't need a new entity type at all, just need to edit the existing one.

Status: Needs review » Needs work

The last submitted patch, 37: 2787881-37.patch, failed testing.

sam152’s picture

Hm, in other issues I thought I ran into issues with entity_test_with_bundle not being revisionable, but if it works, awesome.

Edit: Ah, I can see you are making the entity revisionable. It looks like we have a few tests popping up that require that functionality. Is there really much overhead to a few new test entity types that cover the cross-section of entity features that we need? Altering the entity definitions in that manner might become tedious?

timmillwood’s picture

For small things, like entity key changes altering makes much more sense to me because there are so many possible combinations if we start adding:

  • revisionable with langcode
  • non-revisionable with langcode
  • revisionable without langcode
  • non-revisionable withouth langcode
  • revisionable with langcode and translatable
  • non-revisionable with langcode and translatable

etc

sam152’s picture

Status: Needs work » Reviewed & tested by the community

Looks like the pattern is already in use, so it's an RTBC from me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 37: 2787881-37.patch, failed testing.

timmillwood’s picture

Status: Needs work » Reviewed & tested by the community

Re-queued #37 as it was only failing due to curl timeout putting back to RTBC.

timmillwood’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new10.99 KB
new3.27 KB

Resolving #35

amateescu’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
    @@ -43,12 +43,14 @@ protected function getModerationState() {
    +        if ($entity->getEntityType()->isTranslatable()) {
    

    Missed a spot :)

  2. +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
    @@ -211,6 +218,90 @@ public function testMultilingualModeration() {
    +  public function testNonTranslatableEntityTypeModeration() {
    ...
    +  public function testNonLangcodeEntityTypeModeration() {
    

    The new test method is an exact copy of the other one, shouldn't there be a difference somewhere?

timmillwood’s picture

testNonTranslatableEntityTypeModeration is not translatable, but has a langcode entity key.
testNonLangcodeEntityTypeModeration is not translatable, and doesn't have a langcode entity key.

Other than that it's the same test. So testing the same steps with or without a langcode. I wanted to make sure that if the condition is based on langcode now that is still works for non-translatable entity types if there is a langcode.

amateescu’s picture

Ah, okay, maybe you can put that explanation in the patch as well? And point 1) still stands.

timmillwood’s picture

Status: Needs work » Needs review
StatusFileSize
new10.99 KB
new849 bytes

Fixing #45.

timmillwood’s picture

StatusFileSize
new11.04 KB
new1.11 KB

Updating the comments on the tests to make it clearer the difference between the two.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Nice, looks good now :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/content_moderation/config/schema/content_moderation.schema.yml
@@ -70,6 +70,22 @@ block_content.type.*.third_party.content_moderation:
+entity_test.entity_test_bundle.*.third_party.content_moderation:
+  type: mapping
+  label: 'Enable moderation states for this entity test type'
+  mapping:
+    enabled:
+      type: boolean
+      label: 'Moderation states enabled'
+    allowed_moderation_states:
+      type: sequence
+      sequence:
+        type: string
+        label: 'Moderation state'
+    default_moderation_state:
+      type: string
+      label: 'Moderation state for new entity test'
+

Imo this being in the content moderation schema shows the problem with the current approach of it handles bundles. This is rendered completely unnecessary by #2779647: Add a workflow component, ui module, and implement it in content moderation. If we want to get this in before that we need to move this schema to a test module because it is just not relevant for regular running of a site using content moderation.

sam152’s picture

I think anything made irrelevant by #2779647 is worth holding off on for the moment.

timmillwood’s picture

My fear is that #2779647: Add a workflow component, ui module, and implement it in content moderation is still a few weeks off. Therefore I'll try to reroll this with entity_test.entity_test_bundle.*.third_party.content_moderation: in entity_test module today.

amateescu’s picture

Imo this being in the content moderation schema shows the problem with the current approach of it handles bundles. This is rendered completely unnecessary by #2779647: Add a workflow component, ui module, and implement it in content moderation

This is also *exactly* what made me open and work on #2799785: Entity types with non-config bundles can not be moderated, which was closed in favor of the larger refactoring.

However, that refactoring is mostly better suited for 8.3.x, while this is a genuine 8.2.x bug fix so it's not irrelevant at all.

timmillwood’s picture

Status: Needs work » Needs review
StatusFileSize
new10.96 KB
new1.99 KB

Here's the updated patch with the schema move.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -rc eligible

@alexpott's review has been addressed, back to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 144fe78 to 8.3.x and 1506901 to 8.2.x. Thanks!

@amateescu #2779647: Add a workflow component, ui module, and implement it in content moderation is 8.2.x eligible btw - content_moderation is experimental. The whole point of being in alpha is fix things like this.

diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
index c1eef69..67a7a74 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
@@ -4,9 +4,7 @@
 
 use Drupal\content_moderation\Entity\ContentModerationState;
 use Drupal\content_moderation\Entity\ModerationState;
-use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
 use Drupal\entity_test\Entity\EntityTestBundle;
-use Drupal\entity_test\Entity\EntityTestRev;
 use Drupal\entity_test\Entity\EntityTestWithBundle;
 use Drupal\KernelTests\KernelTestBase;
 use Drupal\language\Entity\ConfigurableLanguage;

Removed unused uses on commit.

  • alexpott committed 144fe78 on 8.3.x
    Issue #2787881 by timmillwood, amateescu, Sam152, dawehner, alexpott:...

  • alexpott committed 1506901 on 8.2.x
    Issue #2787881 by timmillwood, amateescu, Sam152, dawehner, alexpott:...

Status: Fixed » Closed (fixed)

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

timmillwood’s picture

amateescu’s picture

Here's a followup to clean up the tests that were added here: #2898020: Simplify ContentModerationStateTest