Could it be that a workflow state set as "published" or "default revision" do not keep the original status?

$moderation_state_original is empty for that state ( but not in others):

function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() !== 'content_moderation_state') {
return;
}

$moderation_state = $entity->get('moderation_state')->getString();
if ($entity->original) {
$moderation_state_original = $entity->original->get('moderation_state')
->getString();
}
}

How can i retrieve that information?
Best,

Comments

matoeil created an issue. See original summary.

timmillwood’s picture

Issue tags: +Workflow Initiative

I'm not sure I understand the issue?

sam152’s picture

You might need some of the code in #2873287: Dispatch events for changing content moderation states. It loads what the previous status was for the event it dispatches. Word of caution, the content_moderation_state is @internal, so you might experience API breaks in the future implementing hook_entity_presave.

johnwebdev’s picture

I did a work around and stored the current moderation state in the $form_state object on a Node form alter.

/**
 * Implements hook_form_alter().
 */
function hook_form_node_ENTITY_TYPE_edit_form_alter(&$form, FormStateInterface $form_state) {
  // The node object.
  $node = $form_state->getFormObject()->getEntity();
  $form_state->set('current_moderation_state', $node->get('moderation_state')->value);

  $form['actions']['submit']['#submit'][] = ...
}

And then in my custom submission handler I had the correct values. Perhaps you could also attach your submit handler before Moderation State ones, but I didn't bother.
Obviously this would only work if the only way to update a entity state is on its form.

dabbor’s picture

I've observed the same problem when using hook_entity_update().

The documentation for hook_entity_update() https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21... says:

Get the original entity object from $entity->original.

and as the Content Moderation module is a core module (Workflows as well) I would not expect that using Content Moderation for a node is going to break the original entity in the $entity->original. I checked it and the original entity is not the original entity we've just updated, but the "Current revision" instead, meaning the latest published revision (you can find it on the page /node/[nid]/revisions marked as "Current revision"). It may be a problem/bug with revisions in general.

So as a result the $entity->original is not what the documentation says, but the "Current revision" instead. I consider it as a bug as we are getting something else than what is expected (documented) and it is caused by a core module "Content moderation" or Revisioning in core and not by some contrib module. I believe that core modules needs to play nice with the other core modules and the Drupal core itself.

I've tested the value of "moderation_state" in $entity->original and it contains the previous state just fine till the node reaches the "Published" moderation state. Coming back from that state to "Draft" and then to "Needs Review", the "moderation_state" in $entity->original is always "Published" (because of the wrong $entity->original value).

I've had to use the workaround with hook_entity_presave() similar to the comment https://www.drupal.org/project/drupal/issues/2914901#comment-12413964 (#4). That workaround is nicely used by the content_moderation_notifications contrib module https://www.drupal.org/project/content_moderation_notifications and the issue https://www.drupal.org/project/drupal/issues/2873287, as mentioned above, was helpful.

sam152’s picture

I agree, this is probably a bug or at the very least quite confusing. I'm sure it has something to do with the nature of the computed field loading the state on demand. It also doesn't help that the point/behaviour/semantics of $entity->original are kind of ambiguously defined and not a huge focus in testing.

The fact that it's a public property with no documented method/interface would make me inclined to steer away from using it wherever possible.

sam152’s picture

Status: Active » Closed (outdated)
smustgrave’s picture

Version: 8.4.0 » 10.0.x-dev

#5 thank you for pointing to a contrib module that got it working! Anyone know if $entity->original will ever hold the previous moderation state? Or should how content_moderation_notification did it should that be added to core?