diff --git a/composer.json b/composer.json index 42c3639..66ff66b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "drupal/default_content_deploy", - "description": "Drupal 8 module for exporting and deploying content in *.json files. Sponsored by: HBF s.r.o., http://hbf.sk, https://drupal.org/hbf", + "description": "Drupal 9 module for exporting and deploying content in *.json files. Sponsored by: HBF s.r.o., http://hbf.sk, https://drupal.org/hbf", "type": "drupal-module", "authors": [ { @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=5.6.0", + "php": ">=7.0", "rogervila/array-diff-multidimensional": "^2.0" }, "extra": { diff --git a/default_content_deploy.info.yml b/default_content_deploy.info.yml index f16d426..5b95e46 100644 --- a/default_content_deploy.info.yml +++ b/default_content_deploy.info.yml @@ -1,8 +1,8 @@ name: 'Default content deploy' type: module -description: 'Default content deploy solution for Drupal 8. Export and import content continuously.' +description: 'Default content deploy solution for Drupal 9. Export and import content continuously.' package: Web services -core_version_requirement: ^8 || ^9 +core_version_requirement: ^9.1 dependencies: - drupal:hal configure: default_content_deploy.import diff --git a/default_content_deploy.services.yml b/default_content_deploy.services.yml index 92b9a3b..15f4f25 100644 --- a/default_content_deploy.services.yml +++ b/default_content_deploy.services.yml @@ -4,7 +4,7 @@ services: arguments: ['@serializer', '@entity_type.manager', '@hal.link_manager', '@account_switcher', '@default_content_deploy.manager', '@entity.repository', '@cache.default', '@default_content_deploy.exporter'] default_content_deploy.exporter: class: Drupal\default_content_deploy\Exporter - arguments: ['@database', '@default_content_deploy.manager', '@entity_type.manager', '@serializer', '@account_switcher', '@file_system', '@hal.link_manager'] + arguments: ['@database', '@default_content_deploy.manager', '@entity_type.manager', '@serializer', '@account_switcher', '@file_system', '@hal.link_manager', '@event_dispatcher'] default_content_deploy.manager: class: Drupal\default_content_deploy\DeployManager arguments: ['@entity_type.manager', '@config.factory', '@settings', '@file_system', '@request_stack'] diff --git a/src/Event/PreSerializeEvent.php b/src/Event/PreSerializeEvent.php new file mode 100644 index 0000000..e2f6f48 --- /dev/null +++ b/src/Event/PreSerializeEvent.php @@ -0,0 +1,45 @@ +entity = $entity; + $this->mode = $mode; + } + + /** + * @return \Drupal\Core\Entity\ContentEntityInterface + */ + public function getEntity() : ContentEntityInterface { + return $this->entity; + } + + /** + * @return string + */ + public function getMode() { + return $this->mode; + } +} diff --git a/src/Exporter.php b/src/Exporter.php index 97f6026..46060b1 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -2,12 +2,14 @@ namespace Drupal\default_content_deploy; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Session\AccountSwitcherInterface; +use Drupal\default_content_deploy\Event\PreSerializeEvent; use Drupal\hal\LinkManager\LinkManagerInterface; use Symfony\Component\Serializer\Serializer; @@ -133,6 +135,14 @@ class Exporter { */ protected $linkManager; + /** + * The event dispatcher. + * + * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher + */ + protected $eventDispatcher; + + /** * Exporter constructor. * @@ -151,7 +161,7 @@ class Exporter { * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager * The link manager service. */ - public function __construct(Connection $database, DeployManager $deploy_manager, EntityTypeManagerInterface $entityTypeManager, Serializer $serializer, AccountSwitcherInterface $account_switcher, FileSystemInterface $file_system, LinkManagerInterface $link_manager) { + public function __construct(Connection $database, DeployManager $deploy_manager, EntityTypeManagerInterface $entityTypeManager, Serializer $serializer, AccountSwitcherInterface $account_switcher, FileSystemInterface $file_system, LinkManagerInterface $link_manager, ContainerAwareEventDispatcher $eventDispatcher) { $this->database = $database; $this->entityTypeManager = $entityTypeManager; $this->serializer = $serializer; @@ -159,6 +169,7 @@ class Exporter { $this->deployManager = $deploy_manager; $this->fileSystem = $file_system; $this->linkManager = $link_manager; + $this->eventDispatcher = $eventDispatcher; } /** @@ -382,8 +393,8 @@ class Exporter { foreach ($exported_entity_ids as $entity_id) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); - $exported_entity = $this->getSerializedContentWithReferences($entity); - $this->addExportedEntity($exported_entity); + $exported_entities = $this->getSerializedContentWithReferences($entity); + $this->addExportedEntity($exported_entities); } } @@ -468,18 +479,21 @@ class Exporter { * @return $this */ private function addExportedEntity($exported_entity) { + $exported_entity_array = []; + if (is_string($exported_entity)) { $entity = $this->serializer->decode($exported_entity, 'hal_json'); $uuid = $entity['uuid'][0]['value']; - $entity_with_uuid[$uuid] = $exported_entity; $exported_entity_array[$this->entityTypeId][$uuid] = $exported_entity; } - else { + elseif (is_array($exported_entity)) { $exported_entity_array = $exported_entity; } - $this->exportedEntities = array_replace_recursive($this->exportedEntities, $exported_entity_array); + if ($exported_entity_array) { + $this->exportedEntities = array_replace_recursive($this->exportedEntities, $exported_entity_array); + } return $this; } @@ -545,18 +559,24 @@ class Exporter { * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ public function getSerializedContent(ContentEntityInterface $entity) { - if (PHP_SAPI === 'cli') { - $root_user = $this->entityTypeManager->getStorage('user')->load(1); - $this->accountSwitcher->switchTo($root_user); - } + $content = ''; + + $this->eventDispatcher->dispatch(new PreSerializeEvent($entity, $this->mode)); - $host = $this->deployManager->getCurrentHost(); - $this->linkManager->setLinkDomain($host); - $content = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]); - $this->linkManager->setLinkDomain(FALSE); + if ($entity) { + if (PHP_SAPI === 'cli') { + $root_user = $this->entityTypeManager->getStorage('user')->load(1); + $this->accountSwitcher->switchTo($root_user); + } - if (PHP_SAPI === 'cli') { - $this->accountSwitcher->switchBack(); + $host = $this->deployManager->getCurrentHost(); + $this->linkManager->setLinkDomain($host); + $content = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]); + $this->linkManager->setLinkDomain(FALSE); + + if (PHP_SAPI === 'cli') { + $this->accountSwitcher->switchBack(); + } } return $content; @@ -575,28 +595,15 @@ class Exporter { private function getSerializedContentWithReferences(ContentEntityInterface $entity) { $indexed_dependencies = [$entity->uuid() => $entity]; $entities = $this->getEntityReferencesRecursive($entity, 0, $indexed_dependencies); - $host = $this->deployManager->getCurrentHost(); $serialized_entities = []; - $this->linkManager->setLinkDomain($host); - if (PHP_SAPI === 'cli') { - $root_user = $this->entityTypeManager->getStorage('user')->load(1); - $this->accountSwitcher->switchTo($root_user); - } - // Serialize all entities and key them by entity TYPE and uuid. - foreach ($entities as $entity) { + foreach ($entities as $referenced_entity) { $serialize = $this->serializer->serialize($entity, 'hal_json', [ 'json_encode_options' => JSON_PRETTY_PRINT, ]); - $serialized_entities[$entity->getEntityTypeId()][$entity->uuid()] = $serialize; - } - - // Reset the link domain and the current user, if needed. - $this->linkManager->setLinkDomain(FALSE); - if (PHP_SAPI === 'cli') { - $this->accountSwitcher->switchBack(); + $serialized_entities[$entity->getEntityTypeId()][$entity->uuid()] = $this->getSerializedContent($referenced_entity); } return $serialized_entities;