Score:0

Comparing 'old' values in hook_user_update

mw flag

I've got a function which is running on hook_ENTITY_TYPE_update for the user account, but I want to be able to compare against previous values that may have been on the user account, eg see if the user has changed their email address and be able to react to it.

I can see that if I run the debug on the $entity value that is part of the function that there is an 'original' item within the values array, is that the only way to retrieve the pre-edited data?

Score:1
cn flag

Yes, the data prior to the edit is stored in original, which will let you access all the methods of the user object but return values of its prior state.

So, for example, to check whether the email address has been changed:

$previous_email = $account->original->getEmail();

if ($previous_email !== $account->getEmail()) {
Andrew Morris avatar
mw flag
Nice one, I dunno why my IDE kept warning me that I couldn't call $account->original so I never tried running it, but it even says that it's how you get it from here - https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_update/8.2.x
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.