function Exporter::exportReference

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/DefaultContent/Exporter.php \Drupal\Core\DefaultContent\Exporter::exportReference()

Exports an entity reference field item.

Parameters

\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemInterface&\Drupal\Core\Field\FieldItemInterface $item: The field item to export.

\Drupal\Core\DefaultContent\ExportMetadata $metadata: Any metadata about the entity being exported (e.g., dependencies).

Return value

array|null The exported field values, or NULL if no entity is referenced and the item should not be exported.

1 call to Exporter::exportReference()
Exporter::export in core/lib/Drupal/Core/DefaultContent/Exporter.php
Exports a single content entity as an array.

File

core/lib/Drupal/Core/DefaultContent/Exporter.php, line 279

Class

Exporter
Handles exporting content entities.

Namespace

Drupal\Core\DefaultContent

Code

private function exportReference(EntityReferenceItemInterface&FieldItemInterface $item, ExportMetadata $metadata) : ?array {
  $entity = $item->get('entity')
    ->getValue();
  // No entity is referenced, so there's nothing else we can do here.
  if ($entity === NULL) {
    $referencer = $item->getEntity();
    $field_definition = $item->getFieldDefinition();
    $this->logger?->warning('Failed to export reference to @target_type %missing_id referenced by %field on @entity_type %label because the referenced @target_type does not exist.', [
      '@target_type' => (string) $this->entityTypeManager
        ->getDefinition($field_definition->getFieldStorageDefinition()
        ->getSetting('target_type'))
        ->getSingularLabel(),
      '%missing_id' => $item->get('target_id')
        ->getValue(),
      '%field' => $field_definition->getLabel(),
      '@entity_type' => (string) $referencer->getEntityType()
        ->getSingularLabel(),
      '%label' => $referencer->label(),
    ]);
    return NULL;
  }
  $values = $this->exportFieldItem($item);
  if ($entity instanceof ContentEntityInterface) {
    // If the referenced entity is user 0 or 1, we can skip further
    // processing because user 0 is guaranteed to exist, and user 1 is
    // guaranteed to have existed at some point. Either way, there's no chance
    // of accidentally referencing the wrong entity on import.
    if ($entity instanceof AccountInterface && intval($entity->id()) < 2) {
      return array_map('intval', $values);
    }
    // Mark the referenced entity as a dependency of the one we're exporting.
    $metadata->addDependency($entity);
    // If the referenced entity ID is numeric, refer to it by UUID, which is
    // portable. If the ID isn't numeric, assume it's meant to be consistent
    // (like a config entity ID) and leave the reference as-is. Workspaces
    // are an example of an entity type that should be treated this way.
    if ($entity->getEntityType()
      ->hasIntegerId()) {
      $values['entity'] = $entity->uuid();
      unset($values['target_id']);
    }
  }
  return $values;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.