Problem/Motivation

When deleting a moderation state there can still be revision that have been saved in that moderation state. If there are revision saved in that moderation state, and the moderation state is deleted, you will get an error when vising the revision log for that entity like this:

InvalidArgumentException: The state 'need_review' does not exist in workflow. in Drupal\workflows\Plugin\WorkflowTypeBase->getState() (line 155 of /code/web/core/modules/workflows/src/Plugin/WorkflowTypeBase.php).

Steps to reproduce

  1. Install standard
  2. Install Content Moderation
  3. Add Content type
  4. Add content type to Editorial workflow
  5. Create Node with 2 revisions, one in Draft and one in Archived
  6. Delete the archived state
  7. Visit the node revisions page (i.e node/1/revisions)

Expanded stacktrace:

InvalidArgumentException: The state 'archived' does not exist in workflow. in Drupal\workflows\Plugin\WorkflowTypeBase->getState() (line 141 of core/modules/workflows/src/Plugin/WorkflowTypeBase.php).

Drupal\content_moderation\Plugin\WorkflowType\ContentModeration->getState() (Line: 45)
Drupal\content_moderation\StateTransitionValidation->getValidTransitions() (Line: 224)
Drupal\content_moderation\Hook\ContentModerationHooks->entityAccess()
call_user_func_array() (Line: 336)
Drupal\Core\Extension\ModuleHandler->{closure:Drupal\Core\Extension\ModuleHandler::invokeAll():335}() (Line: 287)
Drupal\Core\Extension\ModuleHandler->invokeAllWith() (Line: 335)
Drupal\Core\Extension\ModuleHandler->invokeAll() (Line: 100)
Drupal\Core\Entity\EntityAccessControlHandler->access() (Line: 103)
Drupal\node\NodeAccessControlHandler->access() (Line: 755)
Drupal\Core\Entity\ContentEntityBase->access() (Line: 188)
Drupal\node\Entity\Node->access() (Line: 72)
Drupal\Core\Entity\EntityAccessCheck->access()
call_user_func_array() (Line: 160)
Drupal\Core\Access\AccessManager->performCheck() (Line: 136)
Drupal\Core\Access\AccessManager->check() (Line: 93)
Drupal\Core\Access\AccessManager->checkNamedRoute() (Line: 282)
Drupal\Core\Menu\LocalTaskManager->getTasksBuild() (Line: 324)
Drupal\Core\Menu\LocalTaskManager->getLocalTasks() (Line: 92)
Drupal\Core\Menu\Plugin\Block\LocalTasksBlock->build() (Line: 221)

Proposed resolution

Check the workflow has a state before getting it in getValidTransitions

Remaining tasks

Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3265635

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

matsbla created an issue. See original summary.

sam152’s picture

Component: workflows.module » content_moderation.module
cilefen’s picture

Does the Drupal UI allow deleting states that have revisions or did you delete the state with code?

matsbla’s picture

Yes, the UI allowed to delete the states that have those revisions!

cilefen’s picture

Priority: Normal » Major

Then this can be major priority.

rajandro’s picture

StatusFileSize
new63.69 KB

I have checked the issue in both 9.3.x and 9.4.x. For both scenarios, the functionality is working as expected and the following code is doing this checking perfectly!

    if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getTypePlugin()->getState($this->stateId))) {
      $form['#title'] = $this->getQuestion();
      $form['description'] = ['#markup' => $this->t('This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.')];
      return $form;
    }

@matsbla, If you still get this issue, could you please mention the steps to reproduce it? So I can reverify this again.

Thanks!

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs steps to reproduce

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

el1_1el’s picture

The issue occurs when displaying revisions. If the node with any revision of a content moderation state that has been deleted is shown, the error occurs.

So add node x with workflow y that has states a, b and c. Show the revisions somewhere. Run node x through all 3 states. Then delete step b. Go to the display for node x revisions. You will get the error.

Unfortunately, I have 99 states over 6 workflows with 129 possible transitions (dont ask). I will have to fix the issue by updating content_moderation_state_field_revision with state a or c (or zzz given the volume) in the above scenario

(Updated) A better solution seems to be just deleting the transitions and keeping the legacy states... if you havent deleted them already in production that is.

mcroyle’s picture

StatusFileSize
new201.35 KB

I'm also experiencing this. When I attempt to visit Content -> Moderated Content, I receive an error that an old workflow state does not exist in workflow. None of my nodes are are in this workflow state, however, some had been assigned this state in prior revisions.

Edit: There was a piece of content still in the old workflow state. I was able to re-add the state and the moderation page displayed correctly. I'm not sure how the state was able to be removed originally.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Version: 9.5.x-dev » 11.x-dev
Status: Postponed (maintainer needs more info) » Needs work
Issue tags: +Needs issue summary update

Based on #9 and #10 seems like it could still be a problem

wweibel’s picture

Issue is still present in 10.1.2

An approach would be to default to the 'Default State' of the workflow in these instances. Or when deleting a state require the new state be selected and update all the revisions.

I would prefer the later to avoid stale data being present. Removing the ability to delete states (if there are revisions in that state) is rather heavy handed and virtually guarantees that no state could ever be deleted, and would not reflect changes to human workflow.

Workaround, that can be placed in a post-update-hook:

  $sql = "UPDATE content_moderation_state_field_revision SET moderation_state = :default WHERE moderation_state IN (:states[])";
  \Drupal::database()->query($sql, [':default' => 'DEFAULT', ':states[]' => ['BAD STATE1', 'BAD STATE2', ...]]);
sahana _n’s picture

StatusFileSize
new11.87 MB

Hi

I tried to reproduce the issue in Drupal 11. x but the issue is not reproducible in Drupal 11. x.

I attached the screen recording, please review it.

Please let me know if I made any mistakes.

Thanks!!

phjou’s picture

I confirm I have the same issue, I can delete the workflow state, and then the nodes that were using it have a broken revisions list page.

I approve that the interface should allow to pick a new workflow state to replace the one that has been deleted.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

acbramley’s picture

This was just reported in Diff #3617616: Revision overview fatals when a revision's moderation state no longer exists in the workflow but fixing it there still requires a fix in core. Reproduced easily on main branch.

acbramley’s picture

Issue summary: View changes

Clarifying steps because #14 visits the revision page itself, not the revisions overview.

acbramley’s picture

acbramley’s picture

Status: Needs work » Needs review

AI disclosure - Claude helped write the tests in https://git.drupalcode.org/project/drupal/-/merge_requests/16751/diffs, I have reviewed and manually edited all code afterwards.

MR includes the fix and test coverage for the low level service as well as an integration test ensuring the revisions page doesn't break.

acbramley’s picture

Issue summary: View changes
smustgrave’s picture

per the comment @acbramley made in the MR.