Score:0

How to programatically update a field for a revision?

br flag

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.

Score:1
br flag

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.',
  ];
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.