Generically detecting changed fields in a custom form prior to saving a node I'm adding certain fields from a content type into a custom form using field_attach_form(). When the form is submitted I'm processing those fields by calling field_attach_form_validate() and field_attach_submit() from #validate and #submit callbacks.

At that point, I want to compare the post-submit, prepared node object to the original node and only bother to node_save() it if any of the fields have changed. Therefore, I begin by loading the original node using entity_load_unchanged().

Unfortunately, the field arrays in the original node object don't match the field arrays in the prepared node object which is waiting to be saved, even if no changes have been made to the fields, so a simple "$old_field == $new_field" comparison is impossible. For example, a simple text field appears like this in the original:

$old_node-&gt;field_text['und'][0] = array(
  'value' =&gt; 'Test',
  'format' =&gt; NULL,
  'safe_value' =&gt; 'Test',
);


Whereas in the prepared node it appears like this.

$node-&gt;field_text['und'][0] = array(
  'value' =&gt; 'Test',
);


You might think to just compare the 'value' key, but then you run into fields made up of other elements which don't have 'value' keys. For example, let's look at an address field where there is no 'value' key and there are keys in both the old and prepared nodes which don't have counterparts.

Old node

$old_node-&gt;field_address['und'][0] = array(
  'country' =&gt; 'GB',
  'administrative_area' =&gt; 'Test',
  'sub_administrative_area' =&gt; NULL,
  'locality' =&gt; 'Test',
  'dependent_locality' =&gt; NULL,
  'postal_code' =&gt; 'Test',
  'thoroughfare' =&gt; 'Test',
  'premise' =&gt; 'Test',
  'sub_premise' =&gt; NULL,
  'organisation_name' =&gt; 'Test',
  'name_line' =&gt; 'Test',
  'first_name' =&gt; NULL,
  'last_name' =&gt; NULL,
  'data' =&gt; NULL,
);


Prepared node

$node-&gt;field_address['und'][0] = array(
  'element_key' =&gt; 'node|page|field_address|und|0',
  'thoroughfare' =&gt; 'Test',
  'premise' =&gt; 'Test',
  'locality' =&gt; 'Test',
  'administrative_area' =&gt; 'Test',
  'postal_code' =&gt; 'Test',
  'country' =&gt; 'GB',
  'organisation_name' =&gt; 'Test',
  'name_line' =&gt; 'Test',
);


For empty fields, there is yet another discrepancy.

Old node

$old_node-&gt;field_text = array();


Prepared node

$node-&gt;field_text = array(
  'und' =&gt; array(),
);


Can I generically compare any field's old and new value to detect whether it has changed or not?
Is this just an impossibility?
This, at long last, should work as a generic solution. Thanks to Clive and morbiD for all the input.

Pass both versions of the node to the following function. It will:


Pull all of the detected content type's editable fields and their editable columns (i.e. items that could possibly appear on the custom form) from the database in a single query.
Ignore fields and columns that are completely empty in both versions.
Treat a field that has a different number of values between the two versions as a change.
Iterate through every field, value, and column and compare the two versions.
Compare items non-identically (!=) if they are numeric and identically (!==) if they are anything else.
Immediately return TRUE on the first change it detects (since one change is enough to know we need to resave the node).
Return FALSE if no change is detected after all the values are compared.
Recursively compare field collections by loading them and their schema and passing the results to itself. This SHOULD even allow it to compare nested field collections. The code should NOT have any dependency on the Field Collection module.


Let me know if there are any more bugs or typos in this code.

/*
 * Pass both versions of the node to this function. Returns TRUE if it detects any changes and FALSE if not.
 * Pass field collections as an array keyed by field collection ID.
 *
 * @param object $old_entity
 *   The original (stored in the database) node object.
 *   This function may also pass itself a FieldCollectionItemEntity object to compare field collections.
 * @param object $new_entity
 *   The prepared node object for comparison.
 *   This function may also pass itself a FieldCollectionItemEntity object to compare field collections.
 */
function _fields_changed($old_entity, $new_entity) {
  // Check for node or field collection.
  $entity_is_field_collection = (get_class($old_entity) == 'FieldCollectionItemEntity');

  $bundle = ($entity_is_field_collection ? $old_entity-&gt;field_name : $old_entity-&gt;type);

  // Sanity check. Exit and throw an error if the content types don't match.
  if($bundle !== ($entity_is_field_collection ? $new_entity-&gt;field_name : $new_entity-&gt;type)) {
    drupal_set_message('Content type mismatch. Unable to save changes.', 'error');
    return FALSE;
  }

  // Get field info.
  $field_read_params = array(
    'entity_type' =&gt; ($entity_is_field_collection ? 'field_collection_item' : 'node'),
    'bundle' =&gt; $bundle
  );
  $fields_info = field_read_fields($field_read_params);

  foreach($fields_info as $field_name =&gt; $field_info) {
    $old_field = $old_entity-&gt;$field_name;
    $new_field = $new_entity-&gt;$field_name;

    // Check the number of values for each field, or if they are populated at all.
    $old_field_count = (isset($old_field[LANGUAGE_NONE]) ? count($old_field[LANGUAGE_NONE]) : 0);
    $new_field_count = (isset($new_field[LANGUAGE_NONE]) ? count($new_field[LANGUAGE_NONE]) : 0);

    if ($old_field_count != $new_field_count) {
      // The two versions have a different number of values. Something has changed.
      return TRUE;
    } elseif ($old_field_count &gt; 0 &amp;&amp; $new_field_count &gt; 0) {
      // Both versions have an equal number of values. Time to compare.

      // See if this field is a field collection.
      if ($field_info['type'] == 'field_collection') {

        foreach ($new_field[LANGUAGE_NONE] as $delta =&gt; $values) {
          $old_field_collection = entity_load_unchanged('field_collection_item', $values['entity']-&gt;item_id);
          $new_field_collection = $values['entity'];

          if (_fields_changed($old_field_collection, $new_field_collection)) {
            return TRUE;
          }
        }
        unset($delta, $values);

      } else {
        foreach($old_field[LANGUAGE_NONE] as $delta =&gt; $value) {
          foreach($field_info['columns'] as $field_column_name =&gt; $field_column_info) {
            $old_value = $old_field[LANGUAGE_NONE][$delta][$field_column_name];
            $new_value = $new_field[LANGUAGE_NONE][$delta][$field_column_name];
            $field_column_type = $field_column_info['type'];

            // As with the overall field, exit if one version has a value and the other doesn't.
            if (isset($old_value) != isset($new_value)) {
              return TRUE;
            } elseif (isset($old_value) &amp;&amp; isset($new_value)) {
              // The column stores numeric data so compare values non-identically.
              if (in_array($field_column_type, array('int', 'float', 'numeric'))) {
                if ($new_value != $old_value) {
                  return TRUE;
                }
              }
              // The column stores non-numeric data so compare values identically,
              elseif ($new_value !== $old_value) {
                return TRUE;
              }
            } else {
              // Included for clarity. Both values are empty so there was obviously no change.
            }
          } 
          unset($field_column_name, $field_column_info);
        }
        unset($delta, $value);
      }
    } else {
      // Included for clarity. Both values are empty so there was obviously no change.
    }
  }
  unset($field_name, $field_info);
  // End of field comparison loop.

  // We didn't find any changes. Don't resave the node.
  return FALSE;
}


Sometimes you are interested in knowing which fields have changed.
In order to know that, you can use this version of the function:

/*
 * Pass both versions of the node to this function. Returns an array of
 * fields that were changed or an empty array if none were changed.
 * Pass field collections as an array keyed by field collection ID.
 *
 * @param object $old_entity
 *   The original (stored in the database) node object.
 *   This function may also pass itself a FieldCollectionItemEntity object to compare field collections.
 * @param object $new_entity
 *   The prepared node object for comparison.
 *   This function may also pass itself a FieldCollectionItemEntity object to compare field collections.
 */
function _fields_changed($old_entity, $new_entity) {
  // Check for node or field collection.
  $entity_is_field_collection = (get_class($old_entity) == 'FieldCollectionItemEntity');

  $bundle = ($entity_is_field_collection ? $old_entity-&gt;field_name : $old_entity-&gt;type);

  // Sanity check. Exit and throw an error if the content types don't match.
  if ($bundle !== ($entity_is_field_collection ? $new_entity-&gt;field_name : $new_entity-&gt;type)) {
    drupal_set_message('Content type mismatch. Unable to save changes.', 'error');
    return FALSE;
  }

  // Get field info.
  $field_read_params = array(
    'entity_type' =&gt; ($entity_is_field_collection ? 'field_collection_item' : 'node'),
    'bundle' =&gt; $bundle
  );
  $fields_info = field_read_fields($field_read_params);

  $fields_changed = array();

  foreach ($fields_info as $field_name =&gt; $field_info) {
    $old_field = $old_entity-&gt;$field_name;
    $new_field = $new_entity-&gt;$field_name;

    // Check the number of values for each field, or if they are populated at all.
    $old_field_count = (isset($old_field[LANGUAGE_NONE]) ? count($old_field[LANGUAGE_NONE]) : 0);
    $new_field_count = (isset($new_field[LANGUAGE_NONE]) ? count($new_field[LANGUAGE_NONE]) : 0);

    if ($old_field_count != $new_field_count) {
      // The two versions have a different number of values. Something has changed.
      $fields_changed[] = $field_name;
    }
    elseif ($old_field_count &gt; 0 &amp;&amp; $new_field_count &gt; 0) {
      // Both versions have an equal number of values. Time to compare.

      // See if this field is a field collection.
      if ($field_info['type'] == 'field_collection') {

        foreach ($new_field[LANGUAGE_NONE] as $delta =&gt; $values) {
          $old_field_collection = entity_load_unchanged('field_collection_item', $values['entity']-&gt;item_id);
          $new_field_collection = $values['entity'];

          $fields_changed = array_merge($fields_changed, _fields_changed($old_field_collection, $new_field_collection));
        }
        unset($delta, $values);

      }
      else {
        foreach ($old_field[LANGUAGE_NONE] as $delta =&gt; $value) {
          foreach ($field_info['columns'] as $field_column_name =&gt; $field_column_info) {
            $old_value = $old_field[LANGUAGE_NONE][$delta][$field_column_name];
            $new_value = $new_field[LANGUAGE_NONE][$delta][$field_column_name];
            $field_column_type = $field_column_info['type'];

            // As with the overall field, exit if one version has a value and the other doesn't.
            if (isset($old_value) != isset($new_value)) {
              $fields_changed[] = $old_field;
            }
            elseif (isset($old_value) &amp;&amp; isset($new_value)) {
              // The column stores numeric data so compare values non-identically.
              if (in_array($field_column_type, array(
                'int',
                'float',
                'numeric'
              ))) {
                if ($new_value != $old_value) {
                  $fields_changed[] = $field_name;
                }
              }
              // The column stores non-numeric data so compare values identically,
              elseif ($new_value !== $old_value) {
                $fields_changed[] = $field_name;
              }
            }
            else {
              // Included for clarity. Both values are empty so there was obviously no change.
            }
          }
          unset($field_column_name, $field_column_info);
        }
        unset($delta, $value);
      }
    }
    else {
      // Included for clarity. Both values are empty so there was obviously no change.
    }
  }
  unset($field_name, $field_info);
  // End of field comparison loop.

  return $fields_changed;
}


Sometimes you may want to make it so that changing certain fields of a node do not cause that node's "changed" timestamp to be updated. This could be implemented like as follows:

/**
 * Implements hook_node_presave().
 */
function mymodule_node_presave($node) {
  $fields_changed = _fields_changed($node-&gt;original, $node);
  $no_update_timestamp_fields = array('field_subject', 'field_keywords');
  if (!empty($fields_changed) &amp;&amp;
    empty(array_diff($fields_changed, $no_update_timestamp_fields))) {
    // Don't change the $node-&gt;changed timestamp if one of the fields has
    // been changed that should not affect the timestamp.
    $node-&gt;changed = $node-&gt;original-&gt;changed;
  }
}


EDIT (7/30/2013) Tightened up field collection support. Added support for fields with multiple values.

EDIT (7/31/2015) Added version of function which returns which fields have changed, and example use case.