Check if user_picture value is changed or compare with old value I am trying to find if a field value is changed or updated by the user. But there are blockers.

function custom_form_process_user_picture(&amp;$form, FormStateInterface &amp;$form_state) {
  $file_name = "";
  $username = "";
  $fid_0 = NULL;
  $user_picture = $form['user_picture'];
  $uid = $form_state-&gt;getValue('uid');
  if ($form_state-&gt;hasFileElement() &amp;&amp; $form_state-&gt;hasValue('user_picture')) {
    $username = $form_state-&gt;getValue('field_name')[0]['value'];
    $fid_0 = isset($form_state-&gt;getValue('user_picture')[0]['fids'][0]) ? $form_state-&gt;getValue('user_picture')[0]['fids'][0] : $fid_0;
    /*
      $changed = $form_state-&gt;get('user_picture')-&gt;getValue() == $form_state-&gt;getValue('user_picture');
      //This is not working as $form_state-&gt;get() returns null.
    */
    /*
      $changed = $form_state-&gt;get('user_picture')-&gt;getValue() == $form['user_picture']['#default_value'];
      //This is not working as $form['user_picture']['#default_value'] is null.
    */
      if($changed) {
        // Proceed to rename the file.
      }
    }


What piece of the code I am doing wrong? Searched drupal and stackoverflow for the changed/updated/dirty field value. Also tried with $form_state-&gt;getFormObject()-&gt;getEntity() and got no clue if there is old value stored or not.
FormStateInterface seems to have no method to find out altered/modified values.
Perhaps you should use hook_entity_presave that keeps the old (original) value 

function MODULENAME_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {

  if($entity-&gt;getEntityTypeId() == "file"){

    $filename = $entity-&gt;getFilename();

    $file_old = $entity-&gt;original;

    if($file_old != null){
      $filename_old = $file_old-&gt;getFilename();

      if($filename_old !== $filename){
        $entity-&gt;setFilename('newfile.jpg');
      }
    }
  }
}


The function above changes the filename if its not the same as old filename.

Depending on your use case you might want to apply the above logic when user is presaved

function MODULENAME_entity_presave(Drupal\Core\Entity\EntityInterface $entity)
  if($entity-&gt;getEntityTypeId() == "user"){

    // get specific field by field machine name
    $file = $entity-&gt;get('field_file');

    // or get the user_picture field
    $picture = $entity-&gt;get('user_picture');

    $user_old = $entity-&gt;original;
    // drill down in the old user object to see values changes
    // and do appropriate changes
  }
}