How can I perform an operation programatically when the value of a user field is changed? On my user accounts, I have a text field, field_nickname.  Users can change this at any time.

When they do change it, however, when the user account is saved I want to copy the old value for field_nickname to a text field accepting unlimited values, field_nicknamehistory.

In Drupal 7, you could do this in Rules by making a rule "on user update" and accessing the old value through the tokens in the rule interface.  However, Rules isn't here yet for Drupal 8, so I'm trying to do this in code.

I tried to do this with hook_user_presave() but I don't know how to get the old value to compare it against the new value:

function mymodule_user_presave(Drupal\user\UserInterface $entity) {
  // How do I get $previousnickname?
  if ($entity-&gt;get("field_nickname") !== $previousnickname) {
    $entity-&gt;get('field_nicknamehistory')-&gt;appendItem($entity-&gt;get($previousnickname));
  }


I looked through the API and found onChange(), but I don't see how that would help me here.
The Content translation module has code you could use, right in its hook_entity_presave() implementation (i.e. content_translation_entity_presave()).

if (!isset($entity-&gt;original)) {
  $entity-&gt;original = entity_load_unchanged($entity-&gt;entityType(), $entity-&gt;id());
}


In $entity-&gt;original you then find all the values that entity object had before being changed, as the documentation for entity_load_unchanged() says:


  Loads the unchanged, i.e. not modified, entity from the database.
  
  Unlike entity_load() this function ensures the entity is directly loaded from the database, thus bypassing any static cache. In particular, this function is useful to determine changes by comparing the entity being saved to the stored entity.


If you want to write code that will need less changes when passing to Drupal 9, you can use the following one, which replaces the call to entity_load_unchanged() (marked deprecated) with that function's code.

if (!isset($entity-&gt;original)) {
  $entity-&gt;original = \Drupal::entityManager()-&gt;getStorage($entity-&gt;entityType())-&gt;loadUnchanged($entity-&gt;id());
}


Also image_entity_presave() uses $entity-&gt;original, but it doesn't load it if it has not been set.

$original_uuid = isset($entity-&gt;original) ? $entity-&gt;original-&gt;getSetting('default_image')['uuid'] : NULL;


Probably, you could simplify the code.

if (!isset($entity-&gt;original)) {
  // Get the old value of the field.
}


Looking at the method calling hook_entity_presave(), EntityStorageBase::doPreSave(), the latter seems a more correct code.

  $id = $entity-&gt;id();

  // Track the original ID.
  if ($entity-&gt;getOriginalId() !== NULL) {
    $id = $entity-&gt;getOriginalId();
  }

  // Track if this entity exists already.
  $id_exists = $this-&gt;has($id, $entity);

  // Omissis

  // Load the original entity, if any.
  if ($id_exists &amp;&amp; !isset($entity-&gt;original)) {
    $entity-&gt;original = $this-&gt;loadUnchanged($id);
  }

  // Allow code to run before saving.
  $entity-&gt;preSave($this);
  $this-&gt;invokeHook('presave', $entity);


EntityStorageBase::doPreSave() already sets $entity-&gt;original for entities that aren't new; the code used from content_translation_entity_presave() seems excessive.