You are here

public function DefaultFieldComparator::compareFieldValues in Changed Fields API 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator::compareFieldValues()
  2. 8 src/Plugin/FieldComparator/DefaultFieldComparator.php \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator::compareFieldValues()

Method that compares old and new field values.

Parameters

FieldDefinitionInterface $fieldDefinition:

array $oldValue:

array $newValue:

Return value

array|bool

File

src/Plugin/FieldComparator/DefaultFieldComparator.php, line 142
Contains DefaultFieldComparator.php.

Class

DefaultFieldComparator
Plugin annotation @Plugin( id = "default_field_comparator" )

Namespace

Drupal\changed_fields\Plugin\FieldComparator

Code

public function compareFieldValues(FieldDefinitionInterface $fieldDefinition, array $oldValue, array $newValue) {
  $result = TRUE;
  $properties = $this
    ->getComparableProperties($fieldDefinition);

  // If value was added or removed then we have already different values.
  if (!$oldValue && $newValue || $oldValue && !$newValue) {
    $result = $this
      ->makeResultArray($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)) {
        $result = $this
          ->makeResultArray($oldValue, $newValue);
      }
      else {

        // Walk through each field value and compare it's properties.
        foreach ($newValue as $key => $value) {
          if (is_array($result)) {
            break;
          }
          foreach ($properties as $property) {
            if ($newValue[$key][$property] != $oldValue[$key][$property]) {
              $result = $this
                ->makeResultArray($oldValue, $newValue);
              break;
            }
          }
        }
      }
    }
  }
  return $result;
}