protected function CorrespondingReference::calculateDifferences in Corresponding Entity References 8.4
Return added and removed entities from the provided field.
Parameters
\Drupal\Core\Entity\FieldableEntityInterface $entity: The current entity.
string $fieldName: The field name to check.
bool $deleted: Whether the entity is deleted.
Return value
array The differences keyed by 'added' and 'removed'.
1 call to CorrespondingReference::calculateDifferences()
- CorrespondingReference::synchronizeCorrespondingFields in src/
Entity/ CorrespondingReference.php - Synchronizes corresponding fields on the given entity.
File
- src/
Entity/ CorrespondingReference.php, line 368
Class
- CorrespondingReference
- Defines a corresponding reference entity.
Namespace
Drupal\cer\EntityCode
protected function calculateDifferences(FieldableEntityInterface $entity, $fieldName, $deleted = FALSE) {
/** @var FieldableEntityInterface $original */
$original = isset($entity->original) ? $entity->original : NULL;
$differences = [
CorrespondingReferenceOperations::ADD => [],
CorrespondingReferenceOperations::REMOVE => [],
];
if (!$entity
->hasField($fieldName)) {
return $differences;
}
$entityField = $entity
->get($fieldName);
// If entity is deleted, remove references to it.
if ($deleted) {
/** @var FieldItemInterface $fieldItem */
foreach ($entityField as $fieldItem) {
$differences[CorrespondingReferenceOperations::REMOVE][] = $fieldItem->entity;
}
return $differences;
}
if (empty($original)) {
foreach ($entityField as $fieldItem) {
$differences[CorrespondingReferenceOperations::ADD][] = $fieldItem->entity;
}
return $differences;
}
$originalField = $original
->get($fieldName);
foreach ($entityField as $fieldItem) {
if (!$this
->entityHasValue($original, $fieldName, $fieldItem->target_id)) {
$differences[CorrespondingReferenceOperations::ADD][] = $fieldItem->entity;
}
}
foreach ($originalField as $fieldItem) {
if (!$this
->entityHasValue($entity, $fieldName, $fieldItem->target_id)) {
$differences[CorrespondingReferenceOperations::REMOVE][] = $fieldItem->entity;
}
}
return $differences;
}