You are here

function track_field_changes_compare_field in Track Field Changes 8

Same name and namespace in other branches
  1. 7 track_field_changes.module \track_field_changes_compare_field()

Compare two fields.

return FALSE if different return TRUE if egal

1 call to track_field_changes_compare_field()
track_field_changes_entity_update in ./track_field_changes.module
Implements hook_entity_update().

File

./track_field_changes.module, line 132

Code

function track_field_changes_compare_field($field_info_type, $old_field, $new_field) {
  if ($old_field == NULL && empty($new_field)) {
    return TRUE;
  }
  if ($new_field == NULL && empty($old_field)) {
    return TRUE;
  }
  if ($new_field == NULL && $old_field == NULL) {
    return TRUE;
  }
  if (empty($new_field) && empty($old_field)) {
    return TRUE;
  }
  if (count($new_field) != count($old_field)) {
    return FALSE;
  }
  switch ($field_info_type) {
    case 'title':
    case 'datestamp':
    case 'datetime':
    case 'date':
    case 'tablefield':
    case 'text_long':
    case 'list_string':
    case 'list_boolean':
    case 'list_decimal':
    case 'list_float':
    case 'text_with_summary':
    case 'decimal':
    case 'boolean':
    case 'float':
    case 'text':
    case 'string':
    case 'email':
    case 'telephone':
    case 'language':
      for ($i = 0; $i < count($new_field); $i++) {
        if ($old_field[$i]['value'] != $new_field[$i]['value']) {
          return FALSE;
        }
        if (isset($old_field[$i]['value2']) && $old_field[$i]['value2'] != $new_field[$i]['value2']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'email':
      for ($i = 0; $i < count($new_field); $i++) {
        if ($old_field[$i]['email'] != $new_field[$i]['email']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'link_field':
      for ($i = 0; $i < count($new_field); $i++) {
        if (empty($old_field) || empty($new_field)) {
          return FALSE;

          //if they are both empty, that case is handled at the beginning of this function
        }

        // not all link fields have a title, i.e. static title
        if (array_key_exists('title', $old_field[$i]) && array_key_exists('title', $new_field[$i])) {
          if ($old_field[$i]['title'] != $new_field[$i]['title']) {
            return FALSE;
          }
        }
      }
      for ($i = 0; $i < count($new_field); $i++) {
        if ($old_field[$i]['url'] != $new_field[$i]['url']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'image':
    case 'file':
      for ($i = 0; $i < count($new_field); $i++) {
        if ($old_field[$i]['fid'] != $new_field[$i]['fid']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'geofield':
      for ($i = 0; $i < count($new_field); $i++) {
        if ($old_field[$i]['wkt'] != $new_field[$i]['wkt']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'entity_reference':
      for ($i = 0; $i < count($new_field); $i++) {
        if (empty($old_field[$i]) || $old_field[$i]['target_id'] != $new_field[$i]['target_id']) {
          return FALSE;
        }
      }
      return TRUE;
    case 'address':
      for ($i = 0; $i < count($new_field); $i++) {

        //do a compare of each part of address field

        // first get a list of the address parts
        $address_items = array_keys($new_field[$i]);

        // use $new_field in case something has change in the address definition
        foreach ($address_items as $address_item) {
          if ($address_item != 'data') {
            if (empty($new_field[$i][$address_item])) {
              if (array_key_exists($address_item, $old_field[$i])) {
                if (!empty($old_field[$i][$address_item])) {
                  return FALSE;
                }
              }
              else {
                return FALSE;
              }
            }
            else {
              if (empty($old_field[$i][$address_item])) {
                return FALSE;
              }
              else {
                if ($old_field[$i][$address_item] != $new_field[$i][$address_item]) {
                  return FALSE;
                }
              }
            }
          }
        }
      }

      // if we made it this far, no changes
      return TRUE;
    default:
      return TRUE;
  }
}