You are here

public function CFDefaultFieldComparator::runFieldComparison in Changed Fields API 7

Same name and namespace in other branches
  1. 7.3 src/FieldComparator/CFDefaultFieldComparator.php \CFDefaultFieldComparator::runFieldComparison()
  2. 7.2 includes/changed_fields.core.inc \CFDefaultFieldComparator::runFieldComparison()

Method that runs comparison of field values.

Parameters

array $fieldInfo:

$oldValue:

$newValue:

Return value

array|bool

File

includes/changed_fields.core.inc, line 202
changed_fields.core.inc file.

Class

CFDefaultFieldComparator
Class CFDefaultFieldComparator.

Code

public function runFieldComparison(array $fieldInfo, $oldValue, $newValue) {
  $similarFields = TRUE;
  if ($fieldInfo['type'] == 'field_collection') {

    // If collection was added or removed then we have already
    // different collections.
    if (!$oldValue && $newValue || $oldValue && !$newValue) {
      $similarFields = $this
        ->makeResultArray($fieldInfo['type'], $oldValue, $newValue);
    }
    else {
      if ($oldValue && $newValue) {

        // If value was added|removed to|from multi-value field then we have
        // already different values.
        if (count($newValue) != count($oldValue)) {
          $similarFields = $this
            ->makeResultArray($fieldInfo['type'], $oldValue, $newValue);
        }
        else {
          foreach ($oldValue as $key => $fc) {
            if (is_array($similarFields)) {
              break;
            }
            $oldFc = entity_load('field_collection_item', array(
              $fc['value'],
            ));
            $oldFc = reset($oldFc);
            $newFc = $newValue[$key]['entity'];
            $fcFields = field_info_instances('field_collection_item', $fieldInfo['field_name']);
            foreach ($fcFields as $fcFieldName => $fcFieldData) {
              $fcFieldData = field_info_field($fcFieldName);
              $oldFcFieldValue = field_get_items('field_collection_item', $oldFc, $fcFieldName);
              $newFcFieldValue = field_get_items('field_collection_item', $newFc, $fcFieldName);
              $similarFields = $this
                ->runFieldComparison($fcFieldData, $oldFcFieldValue, $newFcFieldValue);

              // If changes have been detected.
              if (is_array($similarFields)) {

                // Make result array with old and new
                // field collection entities.
                $similarFields = $this
                  ->makeResultArray($fieldInfo['type'], $oldValue, $newValue);
                break;
              }
            }
          }
        }
      }
    }
  }
  else {
    $similarFields = $this
      ->compareFieldValues($fieldInfo['type'], $oldValue, $newValue);
  }
  return $similarFields;
}