Problem/Motivation
Moving from D10 -> D11, content moderation is now validating old revision incorrectly.
If there are revisions of entity with moderation states like this (revision/state)
- 31 / draft
- 32 / published
- 33 / published
and we load revision 32, set it to Archived and validate the entity, violation shows
Invalid state transition from Archived to Archived
Steps to reproduce
With few steps, this can be reproduced in vanilla Drupal.
- Install vanilla Drupal.
- Enable
content_moderation and workflows modules
- Modify the example workflow (Editorial) with following
-
- Enable for Article node type
- Modify Archive transition so that it allows from both Draft and Published to be moved to Archived
- Run sample script with
drush scr
// This part is just to remove the user has no access violation.
/** @var \Drupal\Core\Session\AccountSwitcher $userService */
$userService = \Drupal::service('account_switcher');
$user = \Drupal\user\Entity\User::load(1);
$userService->switchTo($user);
// From now on the actual bug.
$entityTypeManager = \Drupal::entityTypeManager();
$storage = $entityTypeManager->getStorage('node');
// Create draft.
$node= $storage->create(['type' => 'article', 'title' => 'Foobar']);
$node->save();
// Fetch draft point.
$oldId = $node->getRevisionId();
// Set draft -> published. Allowed.
$node->set('moderation_state', 'published');
$node->setNewRevision(TRUE);
$node->save();
// Load draft point.
$node = $storage->loadRevision($oldId);
// Set draft -> archived. Allowed
$node->set('moderation_state', 'archived');
foreach ($node->validate() as $error) {
if ($error->getPropertyPath() == 'moderation_state') {
// Violation for trying to set archived -> archived. Not allowed.
var_dump($error->getMessage());
}
}
Proposed resolution
Correctly load unchanged revision when validating changes.
Remaining tasks
Comments
Comment #2
dropa commentedHere's quick patch for anyone who happens to be stuck with this issue.
This likely still would need test to be created, the script in description might work as skeleton. Moving for in review already to hear if I've completely misunderstood the change from D10 -> D11 and this was somehow intended change.
Comment #3
smustgrave commentedHello thank you for reporting.
Fixes need to be in MRs please landing in main first and depending when they do it can be backported.
Also bugs should have test coverage please
Thanks!
Comment #4
dropa commentedComment #7
sivaji_ganesh_jojodae commentedI've ported the patch #1 to MR.