You are here

public function CorrespondingReference::synchronizeCorrespondingFields in Corresponding Entity References 8.4

Synchronizes corresponding fields on the given entity.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity.

bool $deleted: Whether the entity is deleted.

Overrides CorrespondingReferenceInterface::synchronizeCorrespondingFields

File

src/Entity/CorrespondingReference.php, line 241

Class

CorrespondingReference
Defines a corresponding reference entity.

Namespace

Drupal\cer\Entity

Code

public function synchronizeCorrespondingFields(FieldableEntityInterface $entity, $deleted = FALSE) {
  if (!$this
    ->isValid($entity)) {
    return;
  }
  foreach ($this
    ->getCorrespondingFields() as $fieldName) {
    if (!$entity
      ->hasField($fieldName)) {
      continue;
    }
    $differences = $this
      ->calculateDifferences($entity, $fieldName, $deleted);
    $correspondingField = $this
      ->getCorrespondingField($fieldName);

    // Let other modules alter differences.
    \Drupal::moduleHandler()
      ->alter('cer_differences', $entity, $differences, $correspondingField);
    foreach ($differences as $operation => $entities) {

      /** @var FieldableEntityInterface $correspondingEntity */
      foreach ($entities as $correspondingEntity) {
        if ($correspondingEntity) {
          $this
            ->synchronizeCorrespondingField($entity, $correspondingEntity, $correspondingField, $operation);
        }
      }
    }
  }
}