You are here

public function FieldItemList::equals in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::equals()

Determines equality to another object implementing FieldItemListInterface.

Parameters

\Drupal\Core\Field\FieldItemListInterface $list_to_compare: The field item list to compare to.

Return value

bool TRUE if the field item lists are equal, FALSE if not.

Overrides FieldItemListInterface::equals

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 385
Contains \Drupal\Core\Field\FieldItemList.

Class

FieldItemList
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Field

Code

public function equals(FieldItemListInterface $list_to_compare) {
  $columns = $this
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getColumns();
  $count1 = count($this);
  $count2 = count($list_to_compare);
  if ($count1 === 0 && $count2 === 0) {

    // Both are empty we can safely assume that it did not change.
    return TRUE;
  }
  if ($count1 !== $count2) {

    // One of them is empty but not the other one so the value changed.
    return FALSE;
  }
  $value1 = $this
    ->getValue();
  $value2 = $list_to_compare
    ->getValue();
  if ($value1 === $value2) {
    return TRUE;
  }

  // If the values are not equal ensure a consistent order of field item
  // properties and remove properties which will not be saved.
  $callback = function (&$value) use ($columns) {
    if (is_array($value)) {
      $value = array_intersect_key($value, $columns);
      ksort($value);
    }
  };
  array_walk($value1, $callback);
  array_walk($value2, $callback);
  return $value1 == $value2;
}