Score:-1

Getting old node field value in update hook

kp flag

Hi I'm using the node update hook in my custom .module file in my Drupal 9 site. I have a field called field_amount. And I was wondering if I can access the old value of that field within my update hook and the new value too?

function custom_module_node_update(\Drupal\node\NodeInterface $node) {
  //access the old value of field_amount and the new value
}
Score:1
cn flag

As the docs say:

Get the original entity object from $entity->original.

In your case that would be, for example:

function custom_module_node_update(\Drupal\node\NodeInterface $node) {
  $old_field_value = $node->original->field_foo->value;
}
user108721 avatar
kp flag
Thanks 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.