Problem/Motivation

Now that we have a way to query the latest revision of an entity we can simply a bit the code in \Drupal\content_moderation\ModerationInformation::getLatestRevisionId().

Proposed resolution

Do it.

Remaining tasks

Review.

User interface changes

Nope.

API changes

Nope.

Data model changes

Nope.

Comments

amateescu created an issue. See original summary.

amateescu’s picture

Status: Active » Needs review
StatusFileSize
new1.79 KB

I also simplified getDefaultRevisionId() which uses almost the same code.

sam152’s picture

We have a few places throughout core where we've copied this block of code in order to support latest/pending revisions agnostic of content moderation. It would be awesome to see this moved into #2784201: Track the latest_revision and latest_revision_translation_affected ID in the entity data table for revisionable entity types, so we can fix all of these places in one go.

I have some more thoughts on that issue which I'll add shortly, it unblocks a lot of good cleanup and would allow these methods to either disappear or be @deprecated.

Edit: Also, this is awesome :D

sam152’s picture

StatusFileSize
new9.06 KB
new19.74 KB

I've pushed some code to the other issue for review. I think the changes in #2 make sense to keep around if that approach is accepted. I'm hoping those methods will not be as needed in the rest of core, I'm thinking contexts that need the latest revision will simply choose to load it or check isLatestRevision on the entity itself, mitigating the need to call getLatestRevisionId or getDefaultRevisionId in the first place.

There are two other methods on ModerationInformation which would become redundant however, so this issue could be highjacked to cover those.

If you think the other issue is going to be a long road or don't agree with that approach, just tell me to buzz off and I can RTBC #2, which looks like a great clean-up.

The last submitted patch, 4: 2918569-4.patch, failed testing. View results

timmillwood’s picture

I seems a shame to block this issue on #2784201: Track the latest_revision and latest_revision_translation_affected ID in the entity data table for revisionable entity types. The patch in #2 is a nice advancement on it's own, could we push the changes in #4 to a follow up issue?

sam152’s picture

Happy to take the approach in #6, the blocker could take a while to suss out.

timmillwood’s picture

Awesome, might be time to RTBC #2 then?

sam152’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.79 KB
sam152’s picture

timmillwood’s picture

Thanks for the reupload, looks good to me.

+1 for RTBC.

larowlan’s picture

+++ b/core/modules/content_moderation/src/ModerationInformation.php
@@ -101,13 +99,12 @@ public function getLatestRevisionId($entity_type_id, $entity_id) {
   public function getDefaultRevisionId($entity_type_id, $entity_id) {

excuse my ignorance - but doesn't EntityStorageInterface::load() return the default revision? So is this method only to avoid the entity load?

amateescu’s picture

@larowlan, I think this method actually avoids a loadUnchanged() call, which bypasses the entity cache.

larowlan’s picture

Thanks

xjm’s picture

Status: Reviewed & tested by the community » Needs review
--- a/core/modules/content_moderation/src/ModerationInformation.php
+++ b/core/modules/content_moderation/src/ModerationInformation.php

@@ -84,14 +84,12 @@ public function getLatestRevision($entity_type_id, $entity_id) {
+        ->latestRevision()

@@ -101,13 +99,12 @@ public function getLatestRevisionId($entity_type_id, $entity_id) {
+        ->currentRevision()

I've been squinting at this and I can't tell how the code in HEAD is different, yet it's being changed to different things in the patch.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new2.34 KB
new4.13 KB

@xjm, the difference is that in HEAD getDefaultRevisionId() does not call allRevisions() on the entity query, and the query's default behavior is to get the default revision. I added the currentRevision() call in the patch just to make it a bit more explicit.

Looking around a bit I saw that we don't have dedicated test coverage for these two methods so I wrote it. The test-only patch is supposed to pass both in HEAD and with the patch.

  • xjm committed c53606e on 8.5.x
    Issue #2918569 by amateescu, Sam152: Simplify ModerationInformation::...

  • xjm committed 3d1a2a8 on 8.4.x
    Issue #2918569 by amateescu, Sam152: Simplify ModerationInformation::...
xjm’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Fixed

Ah thanks @amateescu, that's what I was missing. Also +1 for the added test coverage; that helps make it clear that the refactor is correct and safe.

Committed and pushed to 8.5.x. I also cherry-picked it to 8.4.x since it's an internal cleanup and Content Moderation is in beta anyway.

Also this marks my 1000th core commit. Hooray!

amateescu’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Fixed » Reviewed & tested by the community

Weeee, congrats @xjm! :)

amateescu’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Reviewed & tested by the community » Fixed

Oops..

Status: Fixed » Closed (fixed)

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