function 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 to a file.

File

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

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) {
    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);
    $entity_type = $entity->getEntityType();
    // 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_type->hasKey('id') && $entity->getFieldDefinition($entity_type->getKey('id'))
      ->getType() === 'integer') {
      $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.