You are here

final public function EntityProcessorBase::export in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::export()
  2. 3.x src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::export()

Export the entity and return the gid if exists, else false.

Parameters

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

Return value

bool|string The gid if exported, False either

File

src/Processors/Entity/EntityProcessorBase.php, line 82

Class

EntityProcessorBase
The entity processor base.

Namespace

Drupal\content_synchronizer\Processors\Entity

Code

public final function export(Entity $entityToExport) {

  // If entity is exportable (content entity)
  if ($entityToExport
    ->getEntityType() instanceof ContentEntityType) {

    // Get the entity gid.
    $gid = $this
      ->getEntityGlobalReference($entityToExport);
    if (isset($entityToExport->contentSynchronizerIsExporting)) {
      return $gid;
    }
    else {
      $dataToExport = [];
      foreach ($this
        ->getEntityTranslations($entityToExport) as $languageId => $translation) {

        // Tag the current entity has exporting in order to avoid circular dependencies.
        $translation->contentSynchronizerIsExporting = TRUE;
        $dataToExport[self::KEY_TRANSLATIONS][$languageId] = $this
          ->getDataToExport($translation);

        // Add changed time.
        if (method_exists($translation, 'getChangedTime')) {
          $dataToExport[self::KEY_TRANSLATIONS][$languageId][ExportEntityWriter::FIELD_CHANGED] = $translation
            ->getChangedTime();
        }

        // Custom alter data.
        \Drupal::moduleHandler()
          ->alter(self::EXPORT_HOOK, $dataToExport[self::KEY_TRANSLATIONS][$languageId], $translation);
      }
      if (!empty($dataToExport)) {
        $entityToExport->contentSynchronizerGid = $dataToExport[ExportEntityWriter::FIELD_GID] = $gid;
        $dataToExport[ExportEntityWriter::FIELD_UUID] = $entityToExport
          ->uuid();
        ExportProcessor::getCurrentExportProcessor()
          ->getWriter()
          ->write($entityToExport, $dataToExport);
        return $gid;
      }
    }
  }
  return FALSE;
}