You are here

function _field_sql_storage_write_compare in Drupal 7

Compare a single field value for both entities and tell us if it changed.

Parameters

array $field: Loaded field structure.

object $entity1: First entity to compare.

object $entity2: Second entity to compare.

Return value

bool True if field value changed, false otherwise.

2 calls to _field_sql_storage_write_compare()
FieldSqlStorageTestCase::testFieldCompareDataModification in modules/field/modules/field_sql_storage/field_sql_storage.test
Tests the expected return values of _field_sql_storage_write_compare().
field_sql_storage_field_storage_write in modules/field/modules/field_sql_storage/field_sql_storage.module
Implements hook_field_storage_write().

File

modules/field/modules/field_sql_storage/field_sql_storage.module, line 493
Default implementation of the field storage API.

Code

function _field_sql_storage_write_compare($field, $entity1, $entity2) {
  $field_name = $field['field_name'];
  if (empty($entity1->{$field_name}) && empty($entity2->{$field_name})) {

    // Both are empty we can safely assume that it did not change.
    return FALSE;
  }
  if (!isset($entity1->{$field_name}) || !isset($entity2->{$field_name})) {

    // One of them is missing but not the other the value changed.
    return TRUE;
  }

  // We need to proceed to deep array comparison, but we cannot do it naively:
  // in most cases the field values come from the edit form, and some Form API
  // widget values that are not field columns may be present. We need to clean
  // up both original and new field values before comparison.
  $items1 = _field_sql_storage_write_compare_filter($field, (array) $entity1->{$field_name});
  $items2 = _field_sql_storage_write_compare_filter($field, (array) $entity2->{$field_name});
  return $items1 != $items2;
}