diff --git a/components/ImportExportBundle/src/bundle/Resources/config/item_value_transformer/transformers.yaml b/components/ImportExportBundle/src/bundle/Resources/config/item_value_transformer/transformers.yaml index 8af6dddc6..465efbd13 100644 --- a/components/ImportExportBundle/src/bundle/Resources/config/item_value_transformer/transformers.yaml +++ b/components/ImportExportBundle/src/bundle/Resources/config/item_value_transformer/transformers.yaml @@ -64,4 +64,4 @@ services: AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\HtmlToTextBlockTransformer: tags: - - { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.html_to_textblock} \ No newline at end of file + - { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.html_to_textblock} diff --git a/components/ImportExportBundle/src/bundle/Resources/config/services.yaml b/components/ImportExportBundle/src/bundle/Resources/config/services.yaml index 7f6ba40e7..f175caedd 100644 --- a/components/ImportExportBundle/src/bundle/Resources/config/services.yaml +++ b/components/ImportExportBundle/src/bundle/Resources/config/services.yaml @@ -40,3 +40,17 @@ services: - console.command AlmaviaCX\Bundle\IbexaImportExport\Reference\ReferenceBag: + + AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentCreator: + arguments: + $repository: '@Ibexa\Contracts\Core\Repository\Repository' + + AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentUpdater: + arguments: + $repository: '@Ibexa\Contracts\Core\Repository\Repository' + + AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentImporter: + arguments: + $repository: '@Ibexa\Contracts\Core\Repository\Repository' + $contentCreator: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentCreator' + $contentUpdater: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentUpdater' diff --git a/components/ImportExportBundle/src/bundle/Resources/config/workflow/component/writer.yaml b/components/ImportExportBundle/src/bundle/Resources/config/workflow/component/writer.yaml index 22a911f7f..ac50ef3fd 100644 --- a/components/ImportExportBundle/src/bundle/Resources/config/workflow/component/writer.yaml +++ b/components/ImportExportBundle/src/bundle/Resources/config/workflow/component/writer.yaml @@ -18,6 +18,7 @@ services: arguments: $repository: '@Ibexa\Contracts\Core\Repository\Repository' $objectAccessorBuilder: '@AlmaviaCX\Bundle\IbexaImportExport\Accessor\Ibexa\ObjectAccessorBuilder' + $contentImporter: '@AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentImporter' tags: - { name: almaviacx.import_export.component, alias: writer.ibexa.content } diff --git a/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/DownloadToTmpTransformer.php b/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/DownloadToTmpTransformer.php index b99c3a896..b0b5d0226 100644 --- a/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/DownloadToTmpTransformer.php +++ b/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/DownloadToTmpTransformer.php @@ -25,10 +25,15 @@ public function transform($value, array $options = []) $tmpFilePath .= '.'.$originalPathInfos['extension']; } + register_shutdown_function(function () use ($tmpFilePath) { + if (file_exists($tmpFilePath)) { + unlink($tmpFilePath); + } + }); file_put_contents( $tmpFilePath, file_get_contents( - $value, + str_replace(' ', '+', $value), false, stream_context_create( [ @@ -41,10 +46,6 @@ public function transform($value, array $options = []) ) ); - register_shutdown_function(function () use ($tmpFilePath) { - unlink($tmpFilePath); - }); - return $tmpFilePath; } } diff --git a/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/ToDateTimeTransformer.php b/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/ToDateTimeTransformer.php index 51c995603..5e9349f1a 100644 --- a/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/ToDateTimeTransformer.php +++ b/components/ImportExportBundle/src/lib/Item/ValueTransformer/Utils/ToDateTimeTransformer.php @@ -16,7 +16,7 @@ protected function transform($value, array $options = []) return null; } - return DateTime::createFromFormat($options['input_format'], $value); + return DateTime::createFromFormat($options['input_format'], (string) $value); } protected function configureOptions(OptionsResolver $optionsResolver) diff --git a/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregator.php b/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregator.php index 98d6e57f3..5f1b337d6 100644 --- a/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregator.php +++ b/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregator.php @@ -9,6 +9,7 @@ use AlmaviaCX\Bundle\IbexaImportExport\Processor\ProcessorInterface; use JMS\TranslationBundle\Annotation\Desc; use Symfony\Component\Translation\TranslatableMessage; +use Throwable; class ProcessorAggregator extends AbstractProcessor implements ProcessorInterface { @@ -18,11 +19,20 @@ class ProcessorAggregator extends AbstractProcessor implements ProcessorInterfac public function processItem($item) { $processors = $this->getProcessors(); - foreach ($processors as $processor) { - $item = ($processor)($item); - if (false === $item) { - return; + try { + foreach ($processors as $processor) { + $item = ($processor)($item); + if (false === $item) { + return; + } + } + } catch (Throwable $e) { + if ($this->getOption('errorBubbling', true)) { + throw $e; } + $this->logger->logException($e); + + return null; } } diff --git a/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregatorOptions.php b/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregatorOptions.php index 9d9dd6eb3..5f4a9a539 100644 --- a/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregatorOptions.php +++ b/components/ImportExportBundle/src/lib/Processor/Aggregator/ProcessorAggregatorOptions.php @@ -11,11 +11,14 @@ /** * @property array $processors + * @property bool $errorBubbling */ class ProcessorAggregatorOptions extends ProcessorOptions { use ProcessorReferenceAggregationTrait; + protected bool $errorBubbling = true; + public function merge(ComponentOptions $overrideOptions): ComponentOptions { dd($overrideOptions); diff --git a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/AbstractIbexaContentHandler.php b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/AbstractIbexaContentHandler.php new file mode 100644 index 000000000..40a30f5c3 --- /dev/null +++ b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/AbstractIbexaContentHandler.php @@ -0,0 +1,39 @@ +repository = $repository; + } + + /** + * @param array $fieldsByLanguages + */ + protected function setContentFields( + ContentType $contentType, + ContentStruct $contentStruct, + array $fieldsByLanguages + ): void { + foreach ($fieldsByLanguages as $languageCode => $fields) { + foreach ($fields as $fieldID => $field) { + $fieldDefinition = $contentType->getFieldDefinition($fieldID); + if ($fieldDefinition instanceof FieldDefinition) { + $contentStruct->setField($fieldID, $field, $languageCode); + } + } + } + } +} diff --git a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentCreator.php b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentCreator.php new file mode 100644 index 000000000..f0ef1673c --- /dev/null +++ b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentCreator.php @@ -0,0 +1,95 @@ + $parentLocationIdList + * @param array $fieldsByLanguages + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException + */ + public function __invoke( + string $contentTypeIdentifier, + array $parentLocationIdList, + array $fieldsByLanguages, + string $remoteId, + int $ownerId = null, + string $languageCode = 'eng-GB', + int $sectionId = null, + $modificationDate = null, + bool $hidden = false + ): Content { + $contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier( + $contentTypeIdentifier + ); + + /* Creating new content create structure */ + $contentCreateStruct = $this->repository->getContentService()->newContentCreateStruct( + $contentType, + $languageCode + ); + $contentCreateStruct->remoteId = $remoteId; + $contentCreateStruct->ownerId = $ownerId; + if (null !== $modificationDate) { + $contentCreateStruct->modificationDate = $modificationDate instanceof DateTime ? + $modificationDate : + DateTime::createFromFormat('U', (string) $modificationDate); + } + + if ($sectionId) { + $contentCreateStruct->sectionId = $sectionId; + } + + /* Update content structure fields */ + $this->setContentFields($contentType, $contentCreateStruct, $fieldsByLanguages); + + /* Assigning the content locations */ + $locationCreateStructs = []; + foreach ($parentLocationIdList as $locationRemoteId => $parentLocationId) { + if (empty($parentLocationId)) { + throw new Exception('Parent location id cannot be empty'); + } + if ($parentLocationId instanceof Location) { + $parentLocationId = $parentLocationId->id; + } + if (is_string($parentLocationId)) { + $parentLocationId = $this->repository->getLocationService()->loadLocationByRemoteId( + $parentLocationId + )->id; + } + $locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct( + $parentLocationId + ); + if (is_string($locationRemoteId)) { + $locationCreateStruct->remoteId = $locationRemoteId; + } + if ($hidden) { + $locationCreateStruct->hidden = true; + } + $locationCreateStructs[] = $locationCreateStruct; + } + + /* Creating new draft */ + $draft = $this->repository->getContentService()->createContent( + $contentCreateStruct, + $locationCreateStructs + ); + + /* Publish the new content draft */ + return $this->repository->getContentService()->publishVersion($draft->versionInfo); + } +} diff --git a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentImporter.php b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentImporter.php new file mode 100644 index 000000000..931623136 --- /dev/null +++ b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentImporter.php @@ -0,0 +1,81 @@ +contentCreator = $contentCreator; + $this->contentUpdater = $contentUpdater; + $this->repository = $repository; + } + + /** + * @param \AlmaviaCX\Bundle\IbexaImportExport\Writer\Ibexa\Content\IbexaContentData $contentData + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException + * + * @return \Ibexa\Contracts\Core\Repository\Values\Content\Content + */ + public function __invoke(IbexaContentData $contentData, bool $allowUpdate = true) + { + $remoteId = $contentData->getContentRemoteId(); + $ownerId = $contentData->getOwnerId(); + if (null === $ownerId) { + $ownerId = $this->repository + ->getPermissionResolver() + ->getCurrentUserReference() + ->getUserId(); + } + + try { + try { + $content = $this->repository->getContentService()->loadContentByRemoteId( + $contentData->getContentRemoteId() + ); + if (!$allowUpdate) { + return $content; + } + + return ($this->contentUpdater)( + $content, + $contentData->getFields(), + $ownerId, + $contentData->getMainLanguageCode() + ); + } catch (NotFoundException $exception) { + return ($this->contentCreator)( + $contentData->getContentTypeIdentifier(), + $contentData->getParentLocationIdList(), + $contentData->getFields(), + $remoteId, + $ownerId, + $contentData->getMainLanguageCode(), + $contentData->getSectionId(), + $contentData->getModificationDate() + ); + } + } catch (\Throwable $exception) { + dump($exception); + throw $exception; + } + } +} diff --git a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentUpdater.php b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentUpdater.php new file mode 100644 index 000000000..fd8de2547 --- /dev/null +++ b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentUpdater.php @@ -0,0 +1,55 @@ + $fieldsByLanguages + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException + */ + public function __invoke( + Content $content, + array $fieldsByLanguages, + int $ownerId = null, + string $mainLanguageCode = 'eng-GB' + ): Content { + $contentType = $this->repository->getContentTypeService()->loadContentType( + $content->contentInfo->contentTypeId + ); + + $contentInfo = $content->contentInfo; + $contentDraft = $this->repository->getContentService()->createContentDraft($contentInfo); + + /* Creating new content update structure */ + $contentUpdateStruct = $this->repository + ->getContentService() + ->newContentUpdateStruct(); + $contentUpdateStruct->initialLanguageCode = $mainLanguageCode; // set language for new version + $contentUpdateStruct->creatorId = $ownerId; + + $this->setContentFields( + $contentType, + $contentUpdateStruct, + $fieldsByLanguages, + ); + + $contentDraft = $this->repository->getContentService()->updateContent( + $contentDraft->versionInfo, + $contentUpdateStruct + ); + + /* Publish the new content draft */ + return $this->repository->getContentService()->publishVersion($contentDraft->versionInfo); + } +} diff --git a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentWriter.php b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentWriter.php index 6e1a97970..96fc237af 100644 --- a/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentWriter.php +++ b/components/ImportExportBundle/src/lib/Writer/Ibexa/Content/IbexaContentWriter.php @@ -9,20 +9,8 @@ use AlmaviaCX\Bundle\IbexaImportExport\Item\Transformer\SourceResolver; use AlmaviaCX\Bundle\IbexaImportExport\Reference\ReferenceBag; use AlmaviaCX\Bundle\IbexaImportExport\Writer\AbstractWriter; -use DateTime; -use Exception; -use Ibexa\Contracts\Core\Repository\Exceptions\BadStateException; use Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException; -use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; -use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException; use Ibexa\Contracts\Core\Repository\Repository; -use Ibexa\Contracts\Core\Repository\Values\Content\Content; -use Ibexa\Contracts\Core\Repository\Values\Content\ContentStruct; -use Ibexa\Contracts\Core\Repository\Values\Content\Location; -use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; -use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition; -use Ibexa\Core\Base\Exceptions\ContentValidationException; -use Ibexa\Core\Base\Exceptions\InvalidArgumentType; use JMS\TranslationBundle\Model\Message; use JMS\TranslationBundle\Translation\TranslationContainerInterface; use Symfony\Component\Translation\TranslatableMessage; @@ -30,16 +18,19 @@ class IbexaContentWriter extends AbstractWriter implements TranslationContainerInterface { protected Repository $repository; + protected IbexaContentImporter $contentImporter; protected ObjectAccessorBuilder $objectAccessorBuilder; public function __construct( Repository $repository, + IbexaContentImporter $contentImporter, ObjectAccessorBuilder $objectAccessorBuilder, SourceResolver $sourceResolver, ItemTransformer $itemTransformer, ReferenceBag $references ) { $this->repository = $repository; + $this->contentImporter = $contentImporter; $this->objectAccessorBuilder = $objectAccessorBuilder; parent::__construct($sourceResolver, $itemTransformer, $references); } @@ -60,77 +51,28 @@ protected function writeItem($item, $mappedItem) $options = $this->getOptions(); $content = $this->repository->sudo(function (Repository $repository) use ($options, $mappedItem) { - $remoteId = $mappedItem->getContentRemoteId(); - $ownerId = $mappedItem->getOwnerId(); - if (null === $ownerId) { - $ownerId = $this->repository - ->getPermissionResolver() - ->getCurrentUserReference() - ->getUserId(); - } - try { - $content = $repository->getContentService()->loadContentByRemoteId($mappedItem->getContentRemoteId()); - if (!$options->allowUpdate) { - return $content; - } - - return $this->updateContent( - $content, - $mappedItem->getFields(), - $ownerId, - $mappedItem->getMainLanguageCode() + return ($this->contentImporter)($mappedItem, $options->allowUpdate); +// } catch ( Throwable $exception) { +// dd($item, $mappedItem, $exception); + } catch (ContentFieldValidationException $exception) { + $newException = \Ibexa\Core\Base\Exceptions\ContentFieldValidationException::createNewWithMultiline( + $exception->getFieldErrors(), + $mappedItem->getContentRemoteId() ); - } catch (InvalidArgumentType $exception) { -// dd($item, $mappedItem, $exception->getMessage()); - $this->logger->info($exception->getMessage()); - } catch (BadStateException $exception) { - $this->logger->info('Removing content with remote id "'.$remoteId.'"'); - $repository->getContentService()->deleteContent($content->contentInfo); - } catch (UnauthorizedException $exception) { - $this->logger->info('Not authorized to load content with remote id "'.$remoteId.'"'); - } catch (NotFoundException $exception) { - $this->logger->info('Creating new content with remote id "'.$remoteId.'"'); - - try { - return $this->createContent( - $mappedItem->getContentTypeIdentifier(), - $mappedItem->getParentLocationIdList(), - $mappedItem->getFields(), - $remoteId, - $ownerId, - $mappedItem->getMainLanguageCode(), - $mappedItem->getSectionId(), - $mappedItem->getModificationDate() - ); - } catch (InvalidArgumentType $exception) { - $this->logger->info('----> '.get_class($exception)); - $this->logger->info($exception->getMessage()); - $this->logger->info(print_r($exception->getFile().' Line : '.$exception->getLine(), true)); - - throw $exception; - } catch (ContentFieldValidationException $exception) { -// dd($item, $mappedItem, $exception->getFieldErrors()); - $newException = \Ibexa\Core\Base\Exceptions\ContentFieldValidationException::createNewWithMultiline( - $exception->getFieldErrors(), - $remoteId - ); - $this->logger->info('----> '.get_class($newException)); - $this->logger->info($newException->getMessage()); - $this->logger->info(print_r($newException->getFieldErrors(), true)); - $this->logger->info(print_r($newException->getTraceAsString(), true)); - - throw $newException; - } - } catch (ContentValidationException $exception) { - $this->logger->info('----> '.get_class($exception)); - $this->logger->info($exception->getMessage()); - $this->logger->info(print_r($exception->getTraceAsString(), true)); + $this->logger->notice('----> '.get_class($newException)); + $this->logger->notice($newException->getMessage()); + $this->logger->notice(print_r($newException->getFieldErrors(), true)); + $this->logger->notice(print_r($newException->getTraceAsString(), true)); throw $exception; } }); + $this->logger->info( + 'Imported content "'.$content->contentInfo->name.'" ('.$content->contentInfo->remoteId.')' + ); + $imported_content_ids = $this->results->getResult('imported_content_ids'); $imported_content_ids[] = $content->id; $this->results->setResult('imported_content_ids', $imported_content_ids); @@ -138,155 +80,6 @@ protected function writeItem($item, $mappedItem) return $this->objectAccessorBuilder->buildFromContent($content); } - /** - * @param array $fieldsByLanguages - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException - */ - public function updateContent( - Content $content, - array $fieldsByLanguages, - int $ownerId = null, - string $mainLanguageCode = 'eng-GB' - ): Content { - $contentType = $this->repository->getContentTypeService()->loadContentType( - $content->contentInfo->contentTypeId - ); - - $this->logger->info( - 'Updating existing content "'.$content->contentInfo->name.'" ('.$content->contentInfo->remoteId.')' - ); - - $contentInfo = $content->contentInfo; - $contentDraft = $this->repository->getContentService()->createContentDraft($contentInfo); - - /* Creating new content update structure */ - $contentUpdateStruct = $this->repository - ->getContentService() - ->newContentUpdateStruct(); - $contentUpdateStruct->initialLanguageCode = $mainLanguageCode; // set language for new version - $contentUpdateStruct->creatorId = $ownerId; - - $this->setContentFields( - $contentType, - $contentUpdateStruct, - $fieldsByLanguages, - ); - - $contentDraft = $this->repository->getContentService()->updateContent( - $contentDraft->versionInfo, - $contentUpdateStruct - ); - - /* Publish the new content draft */ - return $this->repository->getContentService()->publishVersion($contentDraft->versionInfo); - } - - /** - * @param array $parentLocationIdList - * @param array $fieldsByLanguages - * - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException - */ - public function createContent( - string $contentTypeIdentifier, - array $parentLocationIdList, - array $fieldsByLanguages, - string $remoteId, - int $ownerId = null, - string $languageCode = 'eng-GB', - int $sectionId = null, - $modificationDate = null, - bool $hidden = false - ): Content { - $contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier( - $contentTypeIdentifier - ); - - /* Creating new content create structure */ - $contentCreateStruct = $this->repository->getContentService()->newContentCreateStruct( - $contentType, - $languageCode - ); - $contentCreateStruct->remoteId = $remoteId; - $contentCreateStruct->ownerId = $ownerId; - if (null !== $modificationDate) { - $contentCreateStruct->modificationDate = $modificationDate instanceof DateTime ? - $modificationDate : - DateTime::createFromFormat('U', (string) $modificationDate); - } - - if ($sectionId) { - $contentCreateStruct->sectionId = $sectionId; - } - - /* Update content structure fields */ - $this->setContentFields($contentType, $contentCreateStruct, $fieldsByLanguages); - - /* Assigning the content locations */ - $locationCreateStructs = []; - foreach ($parentLocationIdList as $locationRemoteId => $parentLocationId) { - if (empty($parentLocationId)) { - throw new Exception('Parent location id cannot be empty'); - } - if ($parentLocationId instanceof Location) { - $parentLocationId = $parentLocationId->id; - } - if (is_string($parentLocationId)) { - $parentLocationId = $this->repository->getLocationService()->loadLocationByRemoteId( - $parentLocationId - )->id; - } - $locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct( - $parentLocationId - ); - if (is_string($locationRemoteId)) { - $locationCreateStruct->remoteId = $locationRemoteId; - } - if ($hidden) { - $locationCreateStruct->hidden = true; - } - $locationCreateStructs[] = $locationCreateStruct; - } - - /* Creating new draft */ - $draft = $this->repository->getContentService()->createContent( - $contentCreateStruct, - $locationCreateStructs - ); - - /* Publish the new content draft */ - return $this->repository->getContentService()->publishVersion($draft->versionInfo); - } - - /** - * @param array $fieldsByLanguages - */ - public function setContentFields( - ContentType $contentType, - ContentStruct $contentStruct, - array $fieldsByLanguages - ): void { - foreach ($fieldsByLanguages as $languageCode => $fields) { - foreach ($fields as $fieldID => $field) { - $fieldDefinition = $contentType->getFieldDefinition($fieldID); - if ($fieldDefinition instanceof FieldDefinition) { - $contentStruct->setField($fieldID, $field, $languageCode); - } - } - } - } - public static function getName(): TranslatableMessage { return new TranslatableMessage('writer.ibexa.content.name', [], 'import_export');