You are here

public function ReferenceUpgrader::upgradeEntityReferences in Inline Entity Form 8

File

src/ReferenceUpgrader.php, line 37

Class

ReferenceUpgrader
Class ReferenceUpgrader

Namespace

Drupal\inline_entity_form

Code

public function upgradeEntityReferences(FieldableEntityInterface $entity) {
  foreach ($entity as $fieldName => $fieldItemList) {
    if ($fieldItemList instanceof EntityReferenceFieldItemListInterface && ($targetEntityType = $fieldItemList
      ->getFieldDefinition()
      ->getSetting('target_type')) && ($itemDefinition = $fieldItemList
      ->getItemDefinition()) && $itemDefinition instanceof FieldItemDataDefinitionInterface && $itemDefinition
      ->getPropertyDefinition('target_id')) {
      foreach ($fieldItemList as $fieldItem) {
        assert($fieldItem instanceof FieldItemInterface);
        if ($targetId = $fieldItem->target_id) {
          if ($targetEntity = $this->entities[$targetEntityType][$targetId] ?? NULL) {
            $fieldItem
              ->setValue($targetEntity);
          }
        }
      }
    }
  }
}