0

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?

5
  • Did you verify you are actually getting value in $changed = $entity->getChangedTime(); ? Apr 19 at 3:04
  • @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.
    – awm
    Apr 19 at 13:54
  • 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.
    – Jaypan
    Apr 19 at 14:06
  • @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
    Apr 19 at 14:53
  • 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.
    – awm
    May 1 at 11:27

1 Answer 1

0

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

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.