0

What is wrong with this approach for updating a field value in a specific revision:

public function myTest() {

  $test = 'mytest33';

  $node_revision = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadRevision(10364);

  $node_revision->set('field_my_field', $test);
  $node_revision->save();

  return [
    '#markup' => 'Test page.',
  ];
}

When I load this simple test page and look at revision 10364 for the node, field_my_field doesn't update.

1 Answer 1

1

I needed to use the setSyncing method, like so:

public function myTest() {

  $test = 'mytest33';

  $node_revision = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadRevision(10364);

  // See https://www.drupal.org/project/drupal/issues/2859042#comment-13083066
  $node_revision->original = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadRevision(10364);

  // See https://www.drupal.org/project/drupal/issues/3335119
  $node_revision->setSyncing(TRUE);

  $node_revision->set('field_my_field', $test);
  $node_revision->save();

  return [
    '#markup' => 'Test page.',
  ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.