You are here

public function Exporter::exportContent in Default Content for D8 2.0.x

Same name and namespace in other branches
  1. 8 src/Exporter.php \Drupal\default_content\Exporter::exportContent()

Exports a single entity as importContent expects it.

Parameters

string $entity_type_id: The entity type ID.

mixed $entity_id: The entity ID to export.

string|null $destination: (optional) A file name to write the exported entity into. File entities also export their files into the same folder.

Return value

string The rendered export as hal.

Overrides ExporterInterface::exportContent

1 call to Exporter::exportContent()
Exporter::exportModuleContent in src/Exporter.php
Exports all of the content defined in a module's info file.

File

src/Exporter.php, line 104

Class

Exporter
A service for handling import of default content.

Namespace

Drupal\default_content

Code

public function exportContent($entity_type_id, $entity_id, $destination = NULL) {
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $entity = $storage
    ->load($entity_id);
  if (!$entity) {
    throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" does not exist', $entity_type_id, $entity_id));
  }
  if (!$entity instanceof ContentEntityInterface) {
    throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" is not a content entity', $entity_type_id, $entity_id));
  }
  $normalized = $this->contentEntityNormalizer
    ->normalize($entity);
  $return = Yaml::encode($normalized);
  if ($destination) {
    $folder = dirname(dirname($destination));
    $this->contentFileStorage
      ->writeEntity($folder, $return, $entity, basename($destination));
  }
  $this->eventDispatcher
    ->dispatch(DefaultContentEvents::EXPORT, new ExportEvent($entity));
  return $return;
}