You are here

public function Exporter::exportContent in Default Content for D8 8

Same name and namespace in other branches
  1. 2.0.x 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.

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 135

Class

Exporter
A service for handling import of default content.

Namespace

Drupal\default_content

Code

public function exportContent($entity_type_id, $entity_id) {
  $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));
  }
  if ($this
    ->isCli()) {
    $root_user = $this->entityTypeManager
      ->getStorage('user')
      ->load(1);
    $this->accountSwitcher
      ->switchTo($root_user);
  }
  $this->linkManager
    ->setLinkDomain($this->linkDomain);
  $return = $this->serializer
    ->serialize($entity, 'hal_json', [
    'json_encode_options' => JSON_PRETTY_PRINT,
  ]);
  $this->eventDispatcher
    ->dispatch(DefaultContentEvents::EXPORT, new ExportEvent($entity));

  // Reset the link domain and the current user, if needed.
  $this->linkManager
    ->setLinkDomain(FALSE);
  if ($this
    ->isCli()) {
    $this->accountSwitcher
      ->switchBack();
  }
  return $return;
}