From 1a0f2df59bfb1db5466971395cfe38ee52c3d27c Mon Sep 17 00:00:00 2001 From: "sws-developers@lists.stanford.edu" Date: Fri, 7 Aug 2020 19:36:46 +0000 Subject: [PATCH 1/6] Back to dev --- stanford_migrate.info.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stanford_migrate.info.yml b/stanford_migrate.info.yml index 15af0cf..683d722 100755 --- a/stanford_migrate.info.yml +++ b/stanford_migrate.info.yml @@ -3,7 +3,7 @@ description: 'Adds more functionality to migrate and migrate plus modules' type: module core_version_requirement: ^8.8 || ^9 package: 'Stanford' -version: 8.x-1.2 +version: 8.x-1.3-dev dependencies: - drupal:migrate - migrate_plus:migrate_plus From e2eff554f0a08850f760836043dd3455f984bdf0 Mon Sep 17 00:00:00 2001 From: pookmish Date: Fri, 21 Aug 2020 13:26:58 -0600 Subject: [PATCH 2/6] D8CORE-2499 Updated composer license (#10) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cb020c3..ad54d76 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "drupal-custom-module", "homepage": "https://github.com/SU-SWS/stanford_migrate", "authors": [], - "license": "GPL-2.0+", + "license": "GPL-2.0-or-later", "minimum-stability": "dev", "prefer-stable": true, "repositories": [ From 1843069beb84c3bcfb18854edc5b147a79f0434f Mon Sep 17 00:00:00 2001 From: pookmish Date: Wed, 2 Sep 2020 12:17:24 -0700 Subject: [PATCH 3/6] Filter out null entity ids (#11) --- src/EventSubscriber/EventsSubscriber.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/EventsSubscriber.php b/src/EventSubscriber/EventsSubscriber.php index 155663c..8396f1a 100644 --- a/src/EventSubscriber/EventsSubscriber.php +++ b/src/EventSubscriber/EventsSubscriber.php @@ -107,10 +107,11 @@ public function postImport(MigrateImportEvent $event) { if (!$id_exists_in_source) { // Find the entity id from the id map. $destination_ids = $id_map->lookupDestinationIds($id_map->currentSource()); + $destination_ids = array_filter(reset($destination_ids)); /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ // $destination_ids should be a single item. - $entities = $entity_storage->loadMultiple(reset($destination_ids)); + $entities = $entity_storage->loadMultiple($destination_ids); switch ($orphan_action) { case self::ORPHAN_DELETE: From fb6cb01149e7d1bdfeb90b5134d9747fbfdc413c Mon Sep 17 00:00:00 2001 From: "sws-developers@lists.stanford.edu" Date: Wed, 9 Sep 2020 23:10:59 +0000 Subject: [PATCH 4/6] Back to dev --- stanford_migrate.info.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stanford_migrate.info.yml b/stanford_migrate.info.yml index c335f27..d32ba4b 100755 --- a/stanford_migrate.info.yml +++ b/stanford_migrate.info.yml @@ -3,7 +3,7 @@ description: 'Adds more functionality to migrate and migrate plus modules' type: module core_version_requirement: ^8.8 || ^9 package: 'Stanford' -version: 8.x-1.3 +version: 8.x-1.4-dev dependencies: - drupal:migrate - migrate_plus:migrate_plus From b8a26fa3cdd47713f547de7e08160bab11699c98 Mon Sep 17 00:00:00 2001 From: pookmish Date: Mon, 5 Oct 2020 15:04:05 -0700 Subject: [PATCH 5/6] Fixed orphan actions (#13) --- src/EventSubscriber/EventsSubscriber.php | 92 ++++++++++++++++++++---- stanford_migrate.services.yml | 2 +- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/EventSubscriber/EventsSubscriber.php b/src/EventSubscriber/EventsSubscriber.php index 8396f1a..55ee18a 100644 --- a/src/EventSubscriber/EventsSubscriber.php +++ b/src/EventSubscriber/EventsSubscriber.php @@ -2,6 +2,9 @@ namespace Drupal\stanford_migrate\EventSubscriber; +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Logger\LoggerChannelFactory; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\migrate\Event\MigrateEvents; use Drupal\migrate\Event\MigrateImportEvent; use Drupal\migrate\Plugin\MigrationInterface; @@ -13,6 +16,8 @@ */ class EventsSubscriber implements EventSubscriberInterface { + use StringTranslationTrait; + /** * If the migration is configured to delete orphans. */ @@ -30,11 +35,34 @@ class EventsSubscriber implements EventSubscriberInterface { */ protected $entityTypeManager; + /** + * Logger channel service. + * + * @var \Drupal\Core\Logger\LoggerChannel|\Drupal\Core\Logger\LoggerChannelInterface + */ + protected $logger; + + /** + * Default cache service. + * + * @var \Drupal\Core\Cache\CacheBackendInterface + */ + protected $cache; + /** * Constructs a new MigrateEventsSubscriber object. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * Entity type manager service. + * @param \Drupal\Core\Logger\LoggerChannelFactory $logger_factory + * Logger factory service. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache + * Default cache service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactory $logger_factory, CacheBackendInterface $cache) { $this->entityTypeManager = $entity_type_manager; + $this->logger = $logger_factory->get('stanford_migrate'); + $this->cache = $cache; } /** @@ -58,6 +86,7 @@ public static function getSubscribedEvents() { */ public function postImport(MigrateImportEvent $event) { $orphan_action = $this->getOrphanAction($event->getMigration()); + // Migration doesn't have a orphan action, ignore it. if (!$orphan_action) { return; @@ -106,25 +135,51 @@ public function postImport(MigrateImportEvent $event) { // it. if (!$id_exists_in_source) { // Find the entity id from the id map. - $destination_ids = $id_map->lookupDestinationIds($id_map->currentSource()); - $destination_ids = array_filter(reset($destination_ids)); + $destination_id = $id_map->lookupDestinationIds($id_map->currentSource()); + + // $destination_id should be a single entity id. + while (is_array($destination_id)) { + $destination_id = reset($destination_id); + } - /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ - // $destination_ids should be a single item. - $entities = $entity_storage->loadMultiple($destination_ids); + if (!$destination_id) { + continue; + } + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $entity_storage->load($destination_id); + if (!$entity) { + continue; + } switch ($orphan_action) { case self::ORPHAN_DELETE: + $this->logger->notice($this->t('Deleted entity since it no longer exists in the source data. Migration: @migration, Entity Type: @entity_type, Label: @label'), [ + '@migration' => $event->getMigration()->label(), + '@entity_type' => $type, + '@label' => $entity->label(), + ]); + // Delete the entity, then the record in the id map. - $entity_storage->delete($entities); + $entity->delete(); break; case self::ORPHAN_UNPUBLISH: - // Unpublish the orphans. - foreach ($entities as $entity) { - if ($entity->hasField($status_key)) { - $entity->set($status_key, 0)->save(); + // Unpublish the orphan only if it is currently published. + if ( + $entity->hasField($status_key) && + $entity->get($status_key)->getString() + ) { + $entity->setNewRevision(); + if ($entity->hasField('revision_log')) { + $entity->set('revision_log', 'Unpublished content since it no longer exists in the source data'); } + $entity->set($status_key, 0)->save(); + + $this->logger->notice($this->t('Unpublished entity since it no longer exists in the source data. Migration: @migration, Entity Type: @entity_type, Label: @label'), [ + '@migration' => $event->getMigration()->label(), + '@entity_type' => $type, + '@label' => $entity->label(), + ]); } break; } @@ -141,10 +196,18 @@ public function postImport(MigrateImportEvent $event) { * @param \Drupal\migrate\Plugin\MigrationInterface $migration * Migration object that finished. * - * @return bool + * @return bool|string * Delete orphans or not. */ protected function getOrphanAction(MigrationInterface $migration) { + $cid = 'stanford_migrate:' . $migration->id(); + // No need to check the contents of the cache. The cache is just a + // temporary flag that the orphan action has recently occurred. This + // will prevent the unnecessary double execution. + if ($this->cache->get($cid)) { + return FALSE; + } + $source_config = $migration->getSourceConfiguration(); // The migration entity should have a `delete_orphans` setting in the @@ -153,6 +216,11 @@ protected function getOrphanAction(MigrationInterface $migration) { // @see \Drupal\stanford_migrate\Plugin\migrate\source\StanfordUrl::getAllIds() if (method_exists($migration->getSourcePlugin(), 'getAllIds')) { + + // Just set a cache that expires in 5 minutes. This will allow us to + // just check if the cache exists so that we don't have to run the + // orphan action more than 1 time. + $this->cache->set($cid, time(), time() + 60 + 5); return $source_config['orphan_action']; } } diff --git a/stanford_migrate.services.yml b/stanford_migrate.services.yml index e6d67b8..34a03fd 100644 --- a/stanford_migrate.services.yml +++ b/stanford_migrate.services.yml @@ -1,6 +1,6 @@ services: stanford_migrate.event_subscriber: class: Drupal\stanford_migrate\EventSubscriber\EventsSubscriber - arguments: ['@entity_type.manager'] + arguments: ['@entity_type.manager', '@logger.factory', '@cache.default'] tags: - { name: event_subscriber } From 6f1c01e0f8365793fb512447140e86cf0f90ca85 Mon Sep 17 00:00:00 2001 From: Ian Monroe Date: Mon, 5 Oct 2020 16:05:25 -0600 Subject: [PATCH 6/6] 8.1.4 --- CHANGELOG.md | 6 ++++++ stanford_migrate.info.yml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da69f5b..033deba 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Stanford Migrate +8.x-1.4 +-------------------------------------------------------------------------------- +_Release Date: 2020-10-05_ + +- Fixed orphan actions (#13) (b8a26fa) + 8.x-1.3 -------------------------------------------------------------------------------- _Release Date: 2020-09-09_ diff --git a/stanford_migrate.info.yml b/stanford_migrate.info.yml index d32ba4b..5d2689d 100755 --- a/stanford_migrate.info.yml +++ b/stanford_migrate.info.yml @@ -3,7 +3,7 @@ description: 'Adds more functionality to migrate and migrate plus modules' type: module core_version_requirement: ^8.8 || ^9 package: 'Stanford' -version: 8.x-1.4-dev +version: 8.x-1.4 dependencies: - drupal:migrate - migrate_plus:migrate_plus