function Exporter::export

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

Exports a single content entity as an array.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to export.

Return value

\Drupal\Core\DefaultContent\ExportResult A read-only value object with the exported entity data, and any metadata that was collected while exporting the entity, including dependencies and attachments.

1 call to Exporter::export()
Exporter::exportToFile in core/lib/Drupal/Core/DefaultContent/Exporter.php
Exports an entity to a YAML file in a directory.

File

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

Class

Exporter
Handles exporting content entities.

Namespace

Drupal\Core\DefaultContent

Code

public function export(ContentEntityInterface $entity) : ExportResult {
  $metadata = new ExportMetadata($entity);
  $event = new PreExportEvent($entity, $metadata);
  $field_definitions = $entity->getFieldDefinitions();
  // Ignore serial (integer) entity IDs by default, along with a number of
  // other keys that aren't useful for default content.
  if ($entity->getEntityType()
    ->hasIntegerId()) {
    $event->setEntityKeyExportable('id', FALSE);
  }
  $event->setEntityKeyExportable('uuid', FALSE);
  $event->setEntityKeyExportable('revision', FALSE);
  $event->setEntityKeyExportable('langcode', FALSE);
  $event->setEntityKeyExportable('bundle', FALSE);
  $event->setEntityKeyExportable('default_langcode', FALSE);
  $event->setEntityKeyExportable('revision_default', FALSE);
  $event->setEntityKeyExportable('revision_created', FALSE);
  // Ignore fields that don't make sense in default content:
  // - `changed` fields aren't needed because default content has no history.
  // - `created` fields aren't needed because default content should be
  //   "created" upon import.
  foreach ($field_definitions as $name => $definition) {
    if (in_array($definition->getType(), [
      'changed',
      'created',
    ], TRUE)) {
      $event->setExportable($name, FALSE);
    }
  }
  // Exported user accounts should include the hashed password.
  $event->setCallback('field_item:password', function (PasswordItem $item) : array {
    return $item->set('pre_hashed', TRUE)
      ->getValue();
  });
  // Ensure that all entity reference fields mark the referenced entity as a
  // dependency of the entity being exported.
  $event->setCallback('field_item:entity_reference', $this->exportReference(...));
  $event->setCallback('field_item:file', $this->exportReference(...));
  $event->setCallback('field_item:image', $this->exportReference(...));
  // Dispatch the event so modules can add and customize export callbacks, and
  // mark certain fields as ignored.
  $this->eventDispatcher
    ->dispatch($event);
  $data = [];
  foreach ($entity->getTranslationLanguages() as $langcode => $language) {
    $translation = $entity->getTranslation($langcode);
    $values = $this->exportTranslation($translation, $metadata, $event->getCallbacks(), $event->getAllowList());
    if ($translation->isDefaultTranslation()) {
      $data['default'] = $values;
    }
    else {
      $data['translations'][$langcode] = $values;
    }
  }
  return new ExportResult($data, $metadata);
}

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