Score:0

Prevent last updated property from changing when updating node in code?

mx flag
awm

I have this code that updates a node in a queue worker:

      $changed = $entity->getChangedTime();
      // Don't update the changed time by 
      $entity->setChangedTime($changed);
      $entity->set('my_field', "my_data");
      $entity->setNewRevision(FALSE);
      $entity->save();
      

Here is a screenshot of xdebug showing changed and after_save value with the relevant code: enter image description here

But no matter what I do, the changed property keeps getting updated to the time of the update.

I tested the functionality in a different contenxt like:

  $node = Node::load($nid);
  $changed_time = new DrupalDateTime('2022-01-01 12:00:00');
  $timestamp = $changed_time->getTimestamp();
  $node->setChangedTime($timestamp);
  $node->save();

and this seem to work.

Why does my code in the queue worker fail to do the same thing? is it because it's updating a field value?

miststudent2011 avatar
fr flag
Did you verify you are actually getting value in `$changed = $entity->getChangedTime();` ?
awm avatar
mx flag
awm
@miststudent2011 yeah. I added a screenshot and of the values and relevant code. Maybe something else is acting on it when fields are being updated.
Jaypan avatar
de flag
The changed time will always updted when saving a node. If you need a different timestamp, you will need to create another timestamp on the entity to track whatever it is you're trying to track.
awm avatar
mx flag
awm
@Jaypan do you know where is that happening? it feels like `$node->setChangedTime($timestamp);` should do it. but you may be right, if it's changed then it's always updating it.
awm avatar
mx flag
awm
I figured this out. The issue is that when I try to keep changed time to the original changed, Drupal overrides it. If I add a second, it works.
Score:0
br flag

There's a module that could help with your request. It's a bit hackish but it works.

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.