public function FieldItemList::equals in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::equals()
Determines equality to another object implementing FieldItemListInterface.
This method is usually used by the storage to check for not computed value changes, which will be saved into the storage.
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
1 call to FieldItemList::equals()
- FieldItemList::hasAffectingChanges in core/
lib/ Drupal/ Core/ Field/ FieldItemList.php - Determines whether the field has relevant changes.
2 methods override FieldItemList::equals()
- LayoutSectionItemList::equals in core/
modules/ layout_builder/ src/ Field/ LayoutSectionItemList.php - Determines equality to another object implementing FieldItemListInterface.
- MapFieldItemList::equals in core/
lib/ Drupal/ Core/ Field/ MapFieldItemList.php - Determines equality to another object implementing FieldItemListInterface.
File
- core/
lib/ Drupal/ Core/ Field/ FieldItemList.php, line 371
Class
- FieldItemList
- Represents an entity field; that is, a list of field item objects.
Namespace
Drupal\Core\FieldCode
public function equals(FieldItemListInterface $list_to_compare) {
$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.
$property_definitions = $this
->getFieldDefinition()
->getFieldStorageDefinition()
->getPropertyDefinitions();
$non_computed_properties = array_filter($property_definitions, function (DataDefinitionInterface $property) {
return !$property
->isComputed();
});
$callback = function (&$value) use ($non_computed_properties) {
if (is_array($value)) {
$value = array_intersect_key($value, $non_computed_properties);
// Also filter out properties with a NULL value as they might exist in
// one field item and not in the other, depending on how the values are
// set. Do not filter out empty strings or other false-y values as e.g.
// a NULL or FALSE in a boolean field is not the same.
$value = array_filter($value, function ($property) {
return $property !== NULL;
});
ksort($value);
}
};
array_walk($value1, $callback);
array_walk($value2, $callback);
return $value1 == $value2;
}