Skip to content

Commit

Permalink
8.1.4
Browse files Browse the repository at this point in the history
- Fixed orphan actions (#13) (b8a26fa)
  • Loading branch information
pookmish authored Oct 5, 2020
2 parents e004005 + 6f1c01e commit 2e2bf44
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
92 changes: 80 additions & 12 deletions src/EventSubscriber/EventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +16,8 @@
*/
class EventsSubscriber implements EventSubscriberInterface {

use StringTranslationTrait;

/**
* If the migration is configured to delete orphans.
*/
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion stanford_migrate.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
dependencies:
- drupal:migrate
- migrate_plus:migrate_plus
Expand Down
2 changes: 1 addition & 1 deletion stanford_migrate.services.yml
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 2e2bf44

Please sign in to comment.