setSyncing(TRUE) prevents saving field value if identical to default revision

Created on 20 January 2023, over 1 year ago
Updated 23 January 2023, over 1 year ago

Hello,
As documented here: https://www.drupal.org/node/3052114
I use $node->setSyncing(TRUE) to be able to update fields values of the latest revision without creating a new revision.
I have noticed that unfortunately, if the value I try to save is the exact same value that the one stored in the default revision, setSyncing(TRUE) will prevent this value from being saved !

Let me explain a little bit more:
I have a node that has already been published at least once. So its default revision is the last published one. It's the one you would get by doing a simple Node::load($nid)...
Since the last published revision, I have created multiple draft versions. So I have more recent revisions, and I make sure to load the latest one when I try to programmatically updated it:

$revision_ids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
$last_revision_id = end($revision_ids);
$last_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($last_revision_id);

and then to update one field value of this latest revision, without creating a new revision, I do this:

$last_revision->field_MY_FIELD = "Some value";
$last_revision->setSyncing(TRUE);
$last_revision->save();

This works, but if "Some value" is already the value stored inside the default revision (last published), the value will not be saved (for the last revision which is the one I try to update).

So you can try to change the value to something different than the default version, witness that it's indeed working, saving and not creating a new revision. Then try to put the exact value stored in last published version, and witness that the value is not updated in the last revision.
However, if you comment setSyncing(TRUE) you are able to save the value even if identical to the one of the default revision.

🐛 Bug report
Status

Closed: duplicate

Version

9.4

Component
Content moderation 

Last updated 1 day ago

Created by

🇫🇷France Nicolas Bouteille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia Sam152

    The only reason setSyncing is required in this case is because content moderation forces a new revision without it. If you disabled moderation, it would not be required and you would trigger the same bug without it.

  • 🇫🇷France Nicolas Bouteille

    That makes sense! I'm gonna try to manually set $entity->original like mentioned in #29

  • Status changed to Closed: duplicate over 1 year ago
  • 🇫🇷France Nicolas Bouteille

    Yes setting $entity->original does work!
    Thanks again for the quick follow up
    Marking as duplicate as suggested :)

Production build 0.71.5 2024