FieldItemList::equals

public FieldItemList::equals(FieldItemListInterface $list_to_compare)

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 380

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;
}
doc_Drupal
2016-10-29 09:11:54
Comments
Leave a Comment

Please login to continue.